1710

Is it possible to query for table names which contain columns being

LIKE '%myName%'

?

Rand Random
  • 7,300
  • 10
  • 40
  • 88
gruber
  • 28,739
  • 35
  • 124
  • 216
  • 19
    There is an amazing plugin for sql server which can search for all object types. sql search http://www.red-gate.com/products/sql-development/sql-search/ – Vbp Nov 06 '14 at 22:19
  • 2
    @vbp: sql-search is great indeed, but like many tools, it does not work with SQL Server 2000 (yes, I am stuck with that at the moment :-/ ) – iDevlop Jun 27 '16 at 08:42
  • 4
    @vbp ApexSQL search for SSMS offers even more – Junchen Liu Dec 14 '16 at 11:11

36 Answers36

2605

Search Tables:

SELECT      c.name  AS 'ColumnName'
            ,t.name AS 'TableName'
FROM        sys.columns c
JOIN        sys.tables  t   ON c.object_id = t.object_id
WHERE       c.name LIKE '%MyName%'
ORDER BY    TableName
            ,ColumnName;

Search Tables and Views:

SELECT      COLUMN_NAME AS 'ColumnName'
            ,TABLE_NAME AS  'TableName'
FROM        INFORMATION_SCHEMA.COLUMNS
WHERE       COLUMN_NAME LIKE '%MyName%'
ORDER BY    TableName
            ,ColumnName;
Vikrant
  • 4,920
  • 17
  • 48
  • 72
AdaTheDev
  • 142,592
  • 28
  • 206
  • 200
  • 26
    @Revious `INFORMATION_SCHEMA` [views included in SQL Server comply with the ISO standard definition for the INFORMATION_SCHEMA.](http://technet.microsoft.com/en-us/library/ms186778.aspx), `sys.columns`, `sys.tables` is Microsoft Sql Server specific. – Tomasito Mar 18 '14 at 21:36
  • 14
    Including `TABLE_SCHEMA` in your select list can be helpful. Still +1 because it's a good answer. – Bernhard Hofmann Oct 22 '14 at 13:27
  • 2
    Can you add the explanation of the differences between the two to your answer as commented by @user3185569 ? – Ryan Gates May 23 '16 at 14:34
  • 2
    The second works also with SQL Server 2000, if you happen to have to use that – iDevlop Jun 27 '16 at 08:40
  • 7
    get a table scheman also: SELECT c.name AS ColName, t.name AS TableName, SCHEMA_NAME(t.schema_id) AS SchemaName .... – Skorunka František Oct 06 '16 at 14:20
  • 1
    I would add `UPPER()` to make search case insensitive. `SELECT COLUMN_NAME AS 'ColumnName' ,TABLE_NAME AS 'TableName' FROM INFORMATION_SCHEMA.COLUMNS WHERE UPPER(COLUMN_NAME) LIKE UPPER('%MyName%') ORDER BY TableName ,ColumnName;` – Developer Marius Žilėnas May 10 '18 at 05:09
  • 1
    I was always taught to never touch the sys tables and use INFORMATION_SCHEMA views for these tasks--it's literally what they are made for! – Robert Sherman Dec 03 '20 at 14:01
  • 1
    @RobertSherman The question was asking about finding *tables* with a given column, which is not what the INFORMATION_SCHEMA view approach gives (it includes tables AND views so you'd need to filter those further if you only want tables). Hence, this answer gives both options to cover both needs – AdaTheDev Dec 03 '20 at 14:45
  • @AdaTheDev all well and good but you can just filter the query against the INFORMATION_SCHEMA view you choose (in this case COLUMNS). You can ask it to show only tables in the WHERE. Again, I think MS themselves strongly advise to not touch the sys tables, and have provided the INFORMATION_SCHEMA views to allow you query that information when needed. – Robert Sherman Dec 04 '20 at 15:09
  • 1
    Want to add that I needed to add `USE [database_name]` above this query to select column names in the desired DB. If you're not getting results that make sense, try that. – zcoop98 Feb 12 '21 at 22:44
  • @zcoop98 _"I needed to add USE [database_name]"_ Yeah, if you're not using the right database, *there is no MySQL command that will return sensible data ever.* – HoldOffHunger Dec 10 '21 at 14:40
520

We can also use the following syntax:-

select * from INFORMATION_SCHEMA.COLUMNS 
where COLUMN_NAME like '%clientid%' 
order by TABLE_NAME
Kjartan
  • 18,591
  • 15
  • 71
  • 96
Khwaza Bandenawaz
  • 5,217
  • 1
  • 13
  • 2
240

SQL Server:

SELECT Table_Name, Column_Name 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = 'YOUR_DATABASE'
AND COLUMN_NAME LIKE '%YOUR_COLUMN%'

Oracle:

SELECT owner, table_name, column_name 
FROM all_tab_columns 
WHERE column_name LIKE '%YOUR_COLUMN_NAME%'
AND OWNER IN ('YOUR_SCHEMA_NAME');
  • SIMPLE AS THAT!! (SQL, PL/SQL)
    I use it ALL the time to find ALL instances of a column name in a given database (schema).
Arulkumar
  • 12,966
  • 14
  • 47
  • 68
Todd Vlk
  • 2,549
  • 1
  • 10
  • 11
131

This should work:

SELECT name 
FROM sysobjects 
WHERE id IN ( SELECT id 
              FROM syscolumns 
              WHERE name like '%column_name%' )
Kjartan
  • 18,591
  • 15
  • 71
  • 96
cichy
  • 10,464
  • 4
  • 26
  • 36
118
select  
        s.[name]            'Schema',
        t.[name]            'Table',
        c.[name]            'Column',
        d.[name]            'Data Type',
        c.[max_length]      'Length',
        d.[max_length]      'Max Length',
        d.[precision]       'Precision',
        c.[is_identity]     'Is Id',
        c.[is_nullable]     'Is Nullable',
        c.[is_computed]     'Is Computed',
        d.[is_user_defined] 'Is UserDefined',
        t.[modify_date]     'Date Modified',
        t.[create_date]     'Date created'
from        sys.schemas s
inner join  sys.tables  t
on s.schema_id = t.schema_id
inner join  sys.columns c
on t.object_id = c.object_id
inner join  sys.types   d
on c.user_type_id = d.user_type_id
where c.name like '%ColumnName%'

This here will give you a little extra information about the schema, tables and columns that you may or may not choose to use extra conditions in your where clause to filter on. For example, if you only wanted to see the fields which must have values add

and c.is_nullable = 0

You could add other conditionals, I also added the columns in the select clause in this vertical manner so it was easy to reorder, remove, rename, or add others based on your needs. Alternately you could search for just tables by using T.Name. Its very customisable.

Enjoy.

live-love
  • 48,840
  • 22
  • 240
  • 204
Francis Rodgers
  • 4,565
  • 8
  • 46
  • 65
  • 4
    d.[max_length] seems a little useless. c.[max_length] is maybe what you meant. But still up vote for getting me there. – user1566694 Nov 20 '17 at 21:44
  • 2
    you can put this at the end of your query "ORDER BY t.name" – Fuat Jul 25 '19 at 14:46
  • 1
    User beware. The above query implies access to the 'sys' object which is not always true. In my case for example that query returned nothing. Instead, using the 'INFORMATION_SCHEMA' worked like a charm. – OrizG Apr 24 '20 at 02:04
  • 1
    Usually with flags 1 is true and 0 is false .. The is_nullable flag works like this also. The columns that are "able to be null" i.e. is_nullable are marked with a 1 and the columns which are "not able to be null" .. i.e. is_nullable is false are marked with a 0 value. Just kind interesting to wrap my mind around .. – Allan F Jun 16 '20 at 03:51
  • 1
    can also switch out sys.tables for sys.views if interested in columns of just views .. – Allan F Jun 16 '20 at 04:03
  • 1
    can add to end of query "ORDER BY 1,2,3" to order by first three returned columns .. – Allan F Jun 16 '20 at 04:03
63

If you’re more into third party tools there a lot of options there such as:

These come in very handy if your database contains encrypted objects (views, procedures, functions) because you can’t easily search for these using system tables.

slayernoah
  • 4,382
  • 11
  • 42
  • 73
Dwoolk
  • 1,491
  • 13
  • 8
61

I don't know why many of you suggest joining with sys.table with sys.columns.

You can use the below code:

SELECT Object_name(object_id) AS TableName,
       *
FROM   sys.columns
WHERE  NAME LIKE '%MyName%' 

or

If you want schema name as well:

SELECT *
FROM   information_schema.columns
WHERE  column_name LIKE '%MyName%' 
user3583912
  • 1,302
  • 1
  • 17
  • 23
  • 2
    They are getting the schema name from sys.tables, maybe that is not an issue for you but it is for a lot of people. – K Kimble Feb 12 '15 at 15:04
  • 1
    Hi, but there is still no need to join with sys.objects for schema name, you can use Select * from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME LIKE '%MyName%' – user3583912 Feb 25 '15 at 00:17
  • 1
    Nice that you point out INFORMATION_SCHEMA.COLUMNS, however if you run EXEC sp_helptext INFORMATION_SCHEMA.COLUMNS you can see that it indeed does the same thing, and a lot of useless other stuff you might not need. – K Kimble Feb 26 '15 at 15:01
43

If you simply want the table name you can run:

select object_name(object_id) from sys.columns
where name like '%received_at%'

If you want the Schema Name as well (which in a lot of cases you will, as you'll have a lot of different schemas, and unless you can remember every table in the database and where it belongs this can be useful) run:

select OBJECT_SCHEMA_NAME(object_id),object_name(object_id) from sys.columns
where name like '%received_at%'

and finally if you want it in a nicer format (although this is where the code (In my opinion) is getting too complicated for easy writing):

select concat(OBJECT_SCHEMA_NAME(object_id),'.',object_name(object_id)) from sys.columns
where name like '%received_at%'

note you can also create a function based on what I have:

CREATE PROCEDURE usp_tablecheck
--Scan through all tables to identify all tables with columns that have the provided string
--Stephen B
@name nvarchar(200)
AS
SELECT CONCAT(OBJECT_SCHEMA_NAME(object_id),'.',object_name(object_id)) AS [Table Name], name AS [Column] FROM sys.columns
WHERE name LIKE CONCAT('%',@name,'%')
ORDER BY [Table Name] ASC, [Column] ASC
GO

It is worth noting that the concat feature was added in 2012. For 2008r2 and earlier use + to concatenate strings.

I've re-formatted the proc a bit since I posted this. It's a bit more advanced now but looks a lot messier (but it's in a proc so you'll never see it) and it's formatted better.

This version allows you to have it in an administrative database and then search through any database. Change the decleration of @db from 'master' to whichever you want the default database to be (NOTE: using the CONCAT() function will only work with 2012+ unless you change the string concatenation to use the + operators).

CREATE PROCEDURE [dbo].[usp_tablecheck]
    --Scan through all tables to identify all tables in the specified database with columns that have the provided string
    --Stephen B
    @name nvarchar(200)
    ,@db nvarchar(200) = 'master'
AS
    DECLARE @sql nvarchar(4000) = CONCAT('
        SELECT concat(OBJECT_SCHEMA_NAME(col.object_id,DB_ID(''',@db,''')),''.'',object_name(col.object_id,DB_ID(''',@db,'''))) AS [Table Name]
            ,col.name AS [Column] 
        FROM ',@db,'.sys.columns col
        LEFT JOIN ',@db,'.sys.objects ob 
            ON ob.object_id = col.object_id
        WHERE 
            col.name LIKE CONCAT(''%'',''',@name,''',''%'') 
            AND ob.type =''U''
        ORDER BY [Table Name] ASC
            ,[Column] ASC')
    EXECUTE (@sql)
GO
bobsbeenjamin
  • 193
  • 3
  • 8
Ste Bov
  • 826
  • 7
  • 10
37
SELECT table_schema + '.' + table_name,
       column_name
FROM   [yourdatabase].information_schema.columns
WHERE  column_name LIKE '%myName%' 

This will give you the table name of the column that you need to find.

Gudwlk
  • 1,177
  • 11
  • 11
32
SELECT COLUMN_NAME, TABLE_NAME
  FROM INFORMATION_SCHEMA.COLUMNS    
 WHERE COLUMN_NAME LIKE '%myName%'
Neil Knight
  • 47,437
  • 25
  • 129
  • 188
30

Here is the answer to your question

SELECT c.name AS ColumnName, t.name AS TableName
FROM sys.columns c
    JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE '%myName%';
Yogendra
  • 504
  • 5
  • 13
29
USE AdventureWorks

GO

SELECT t.name AS table_name, SCHEMA_NAME(schema_id) AS schema_name,
 c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%EmployeeID%'
ORDER BY schema_name, table_name; 

It is from Pinal Sir Blog

Ocaso Protal
  • 19,362
  • 8
  • 76
  • 83
shadab shah
  • 551
  • 1
  • 6
  • 8
26

You can find it from INFORMATION_SCHEMA.COLUMNS by column_name filter

Select DISTINCT TABLE_NAME as TableName,COLUMN_NAME as ColumnName
     From INFORMATION_SCHEMA.COLUMNS Where column_name like '%myname%'
Munavvar
  • 802
  • 1
  • 11
  • 33
24

Following query will give you the exact table names of the database having field name like '%myName'.

SELECT distinct(TABLE_NAME)
  FROM INFORMATION_SCHEMA.COLUMNS    
 WHERE COLUMN_NAME LIKE '%myName%'
Shaikh Farooque
  • 2,620
  • 1
  • 19
  • 33
23

To get full information: column name, table name as well as schema of the table..

SELECT COLUMN_NAME, TABLE_NAME, TABLE_SCHEMA
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE COLUMN_NAME LIKE '%col_Name%'
Alex M
  • 2,756
  • 7
  • 29
  • 35
Kaleab
  • 241
  • 2
  • 4
22
SELECT  [TABLE_NAME] ,
        [INFORMATION_SCHEMA].COLUMNS.COLUMN_NAME
FROM    INFORMATION_SCHEMA.COLUMNS
WHERE   INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME LIKE '%NAME%' ;
Suraj Singh
  • 4,041
  • 1
  • 21
  • 36
17

In MS SQL Server Database, use this query to get the tables and respective column names that contains the input text:

SELECT t.name AS tableName, c.name AS columnName 
FROM sys.tables as t 
INNER JOIN sys.columns AS c ON t.object_id=c.object_id 
WHERE c.name LIKE '%<your_search_string>%'
blackgreen
  • 34,072
  • 23
  • 111
  • 129
dev_2014
  • 321
  • 3
  • 6
15

I wanted something for tables and views that didn't make my eyes bleed.

Query

SELECT
    t.TABLE_TYPE AS [Type],
    c.TABLE_NAME AS [Object],
    c.COLUMN_NAME AS [Column]
FROM
    INFORMATION_SCHEMA.COLUMNS AS c
    LEFT JOIN INFORMATION_SCHEMA.TABLES AS t ON
        t.TABLE_CATALOG = c.TABLE_CATALOG AND 
        t.TABLE_SCHEMA = c.TABLE_SCHEMA AND
        t.TABLE_NAME = c.TABLE_NAME
WHERE
    c.COLUMN_NAME LIKE '%myName%'
ORDER BY
    [Type],
    [Object],
    [Column]

Result

Type        Object   Column
----------------------------
BASE TABLE  Table1   myName1
BASE TABLE  Table2   myName2
VIEW        View1    myName1
VIEW        View2    myName2
MarredCheese
  • 17,541
  • 8
  • 92
  • 91
13

i have just tried it and this works perfectly

USE YourDatabseName
GO
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%YourColumnName%'
ORDER BY schema_name, table_name;

Only change YourDatbaseName to your database and YourcolumnName to your column name that you are looking for the rest keep it as it is.

Hope this has helped

Alz
  • 351
  • 5
  • 17
13

It will return table_name , schema_name , column_name

SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c
ON t.object_id = c.object_id
WHERE c.name LIKE '%colName%'
ORDER BY schema_name,
table_name;
Rinku Choudhary
  • 1,529
  • 1
  • 13
  • 22
9

This simple query worked for me.

SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'schemaName'
AND column_name like '%model%';
Saurabh Rana
  • 3,350
  • 2
  • 19
  • 22
8
DECLARE @columnName as varchar(100)
SET @columnName = 'ColumnName'

SELECT t.name AS Table, c.name AS Column,
ty.name AS Type, c.max_length AS Length
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
INNER JOIN sys.types ty ON c.system_type_id = ty.system_type_id
WHERE c.name LIKE @columnName
ORDER BY t.name, c.name
Mauro Bilotti
  • 5,628
  • 4
  • 44
  • 65
7

I used this for the same purpose and it worked:

  select * from INFORMATION_SCHEMA.COLUMNS
  where TABLE_CATALOG= 'theDatabase'
  and COLUMN_NAME like 'theCol%'
Rainhider
  • 806
  • 1
  • 17
  • 31
7
SELECT t.name AS table_name, 
    SCHEMA_NAME(schema_id) AS schema_name,
    c.name AS column_name
FROM sys.tables AS t INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%Label%'
ORDER BY schema_name, table_name;
slavoo
  • 5,798
  • 64
  • 37
  • 39
Pedro S Cord
  • 1,301
  • 17
  • 20
7

Like oracle you can find tables and columns with this:

select table_name, column_name
from user_tab_columns 
where column_name 
like '%myname%';
ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
Iceknight
  • 113
  • 2
  • 6
7

You can try this query:

USE AdventureWorks
GO
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%myName%'
gligoran
  • 3,267
  • 3
  • 32
  • 47
  • 2
    Looks very similar to https://blog.sqlauthority.com/2008/08/06/sql-server-query-to-find-column-from-all-tables-of-database/ can help with explanation if nothing else. – user3428422 Apr 06 '17 at 07:41
6

Hopefully this isn't a duplicate answer, but what I like to do is generate a sql statement within a sql statement that will allow me to search for the values I am looking for (not just the tables with those field names ( as it's usually necessary for me to then delete any info related to the id of the column name I am looking for):

  SELECT  'Select * from ' + t.name + ' where ' + c.name + ' = 148' AS SQLToRun
  FROM sys.columns c, c.name as ColName, t.name as TableName
  JOIN sys.tables t 
     ON c.object_id = t.object_id
  WHERE c.name LIKE '%ProjectID%'

Then I can copy and paste run my 1st column "SQLToRun"... then I replace the "Select * from ' with 'Delete from ' and it allows me to delete any references to that given ID! Write these results to file so you have them just in case.

NOTE**** Make sure you eliminate any bakup tables prior to running your your delete statement...

  SELECT  'Delete from ' + t.name + ' where ' + c.name + ' = 148' AS SQLToRun
  FROM sys.columns c, c.name as ColName, t.name as TableName
  JOIN sys.tables t 
     ON c.object_id = t.object_id
  WHERE c.name LIKE '%ProjectID%'
Danimal111
  • 1,976
  • 25
  • 31
5
Create table #yourcolumndetails(
DBaseName varchar(100), 
TableSchema varchar(50), 
TableName varchar(100),
ColumnName varchar(100), 
DataType varchar(100), 
CharMaxLength varchar(100))

EXEC sp_MSForEachDB @command1='USE [?];
    INSERT INTO #yourcolumndetails SELECT
    Table_Catalog
    ,Table_Schema
    ,Table_Name
    ,Column_Name
    ,Data_Type
    ,Character_Maximum_Length
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE COLUMN_NAME like ''origin'''

select * from #yourcolumndetails
Drop table #yourcolumndetails
Tasawar
  • 541
  • 4
  • 20
DataWrangler
  • 1,804
  • 17
  • 32
4

Just to improve on the answers above i have included Views as well and Concatenated the Schema and Table/View together making the Results more apparent.

DECLARE @COLUMNNAME AS VARCHAR(100);

SET @COLUMNNAME = '%Absence%';

SELECT CASE
           WHEN [T].[NAME] IS NULL
           THEN 'View'
           WHEN [T].[NAME] = ''
           THEN 'View'
           ELSE 'Table'
       END AS [TYPE], '[' + [S].[NAME] + '].' + '[' + CASE
                                                          WHEN [T].[NAME] IS NULL
                                                          THEN [V].[NAME]
                                                          WHEN [T].[NAME] = ''
                                                          THEN [V].[NAME]
                                                          ELSE [T].[NAME]
                                                      END + ']' AS [TABLE], [C].[NAME] AS [COLUMN]
FROM [SYS].[SCHEMAS] AS [S] LEFT JOIN [SYS].[TABLES] AS [T] ON [S].SCHEMA_ID = [T].SCHEMA_ID
                            LEFT JOIN [SYS].[VIEWS] AS [V] ON [S].SCHEMA_ID = [V].SCHEMA_ID
                            INNER JOIN [SYS].[COLUMNS] AS [C] ON [T].OBJECT_ID = [C].OBJECT_ID
                                                                 OR
                                                                 [V].OBJECT_ID = [C].OBJECT_ID
                            INNER JOIN [SYS].[TYPES] AS [TY] ON [C].[SYSTEM_TYPE_ID] = [TY].[SYSTEM_TYPE_ID]
WHERE [C].[NAME] LIKE @COLUMNNAME
GROUP BY '[' + [S].[NAME] + '].' + '[' + CASE
                                             WHEN [T].[NAME] IS NULL
                                             THEN [V].[NAME]
                                             WHEN [T].[NAME] = ''
                                             THEN [V].[NAME]
                                             ELSE [T].[NAME]
                                         END + ']', [T].[NAME], [C].[NAME], [S].[NAME]
ORDER BY '[' + [S].[NAME] + '].' + '[' + CASE
                                             WHEN [T].[NAME] IS NULL
                                             THEN [V].[NAME]
                                             WHEN [T].[NAME] = ''
                                             THEN [V].[NAME]
                                             ELSE [T].[NAME]
                                         END + ']', CASE
                                                        WHEN [T].[NAME] IS NULL
                                                        THEN 'View'
                                                        WHEN [T].[NAME] = ''
                                                        THEN 'View'
                                                        ELSE 'Table'
                                                    END, [T].[NAME], [C].[NAME];
Stephen Brett
  • 59
  • 1
  • 2
4

SQL query to show all tables that have specified column name:

SELECT SCHEMA_NAME(schema_id) + '.' + t.name AS 'Table Name'
  FROM sys.tables t
 INNER JOIN sys.columns c ON c.object_id = t.object_id
 WHERE c.name like '%ColumnName%'
 ORDER BY 'Table Name'
MartenCatcher
  • 2,713
  • 8
  • 26
  • 39
Raj
  • 111
  • 2
3

To find all tables containing a column with a specified name in MS SQL Server, you can query the system catalog views. Specifically, you can query the sys.tables and sys.columns views. Here's an example query:

SELECT t.name AS table_name
FROM sys.tables t
JOIN sys.columns c ON t.object_id = c.object_id
WHERE c.name = 'column_name';

Replace column_name with the name of the column you want to search for. This query joins the sys.tables and sys.columns views based on the object_id column and filters the result to retrieve the table names where the column name matches the specified value.

In dbForge Studio for SQL Server, you can use the SQL editor to execute the query mentioned above. It offers features like syntax highlighting, code completion, and result viewing, making it easier to write and execute SQL queries efficiently.

Mahum
  • 71
  • 3
2

Here's a working solution for a Sybase database

select 
  t.table_name, 
  c.column_name 
from 
  systab as t key join systabcol as c 
where 
   c.column_name = 'MyColumnName'
Janey
  • 1,260
  • 3
  • 17
  • 39
2

We can use sp_columns for the purpose.

sp_columns 'table name', null, null, '%column name%'
Sheikh Abdul Wahid
  • 2,623
  • 2
  • 25
  • 24
  • Seems like this is limited to one table name and won't help OP find all tables thought might be a building block.https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-columns-transact-sql?view=sql-server-ver16 – arielkaluzhny Dec 21 '22 at 22:10
  • leave table parameter as null. sp_columns null, null, null, '%column name%' – Sheikh Abdul Wahid Mar 27 '23 at 12:13
2

In MS SQL, you can write the below line to check the column names of a particular table:

sp_help your_tablename

Or, you can first select your table name in the query windows (highlight the schema and table name) and then press key combination below:

Alt + F1
jeppoo1
  • 650
  • 1
  • 10
  • 23
Goutam
  • 377
  • 1
  • 2
  • 11
  • OP asked how to find columns matching a criteria across all tables. Not find the names of the columns in a given table. – simo.3792 Nov 01 '22 at 04:42
1

In addition, you can find column names with specified schema also.

SELECT 'DBName' AS DBName,
       column_name,
       table_name,
       table_schema
FROM   dbname.information_schema.columns
WHERE  column_name LIKE '%YourColumnName%'
       AND table_schema IN ( 'YourSchemaName' ) 

You can also find the same column on multiple databases.

SELECT 'DBName1' AS DB,
       column_name,
       table_name,
       table_schema
FROM   dbname1.information_schema.columns
WHERE  column_name LIKE '%YourColumnName%'
UNION
SELECT 'DBName2' AS DB,
       column_name,
       table_name,
       table_schema
FROM   dbname2.information_schema.columns
WHERE  column_name LIKE '%YourColumnName%' 
Majedur
  • 3,074
  • 1
  • 30
  • 43
-1

You can use the query below in SQL Server.

SELECT column_name AS 'ColumnName',
       table_name  AS 'TableName'
FROM   information_schema.columns
WHERE  column_name LIKE '%columnname%'
       AND table_schema = 'schema'
ORDER  BY tablename,
          columnname;
hardik rawal
  • 117
  • 1
  • 2
  • 18