How can I get a list of all the tables that have a specific column name?
Asked
Active
Viewed 1.3e+01k times
8 Answers
108
Pretty simple on a per database level
Use DatabaseName
Select * From INFORMATION_SCHEMA.COLUMNS Where column_name = 'ColName'

Evidica
- 1,464
- 1
- 14
- 19
-
2This solution includes views. Is there a way to determine which ones are tables and which are views? – DanCaveman May 08 '13 at 20:33
-
See this to to get only tables or tables and views [MSSQL] http://stackoverflow.com/questions/4849652/find-all-tables-containing-column-with-specified-name – Shiham Dec 02 '15 at 10:28
-
1Works for MySQL too :) ! – jave.web Mar 04 '16 at 19:40
-
@DanKaufman join as in https://stackoverflow.com/a/30674301/125981 (I know this is old but making it easier for others) – Mark Schultheiss Jun 06 '18 at 19:56
18
select table_name
from information_schema.columns
where COLUMN_NAME = 'MyColumn'

D'Arcy Rittich
- 167,292
- 40
- 290
- 283
10
You can use the information schema views:
SELECT DISTINCT TABLE_SCHEMA, TABLE_NAME
FROM Information_Schema.Columns
WHERE COLUMN_NAME = 'ID'
Here's the MSDN reference for the "Columns" view: http://msdn.microsoft.com/en-us/library/ms188348.aspx

David
- 34,223
- 3
- 62
- 80
7
If you're trying to query an Oracle database, you might want to use
select owner, table_name
from all_tab_columns
where column_name = 'ColName';

avDev
- 71
- 1
- 2
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 '%YOUR_COLUMN%'
ORDER BY schema_name, table_name;

SKocheta
- 133
- 1
- 8
4
SELECT T.TABLE_NAME, C.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS C
INNER JOIN INFORMATION_SCHEMA.TABLES T ON T.TABLE_NAME = C.TABLE_NAME
WHERE TABLE_TYPE = 'BASE TABLE'
AND COLUMN_NAME = 'ColName'
This returns tables only and ignores views for anyone who is interested!

OneSimpleGeek
- 138
- 11
1
--get tables that contains selected columnName
SELECT c.name AS ColName, t.name AS TableName
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE '%batchno%'
its worked...

Er Ketan Vavadiya
- 283
- 3
- 14
1
You can find what you're looking for in the information schema: SQL Server 2005 System Tables and Views I think you need SQL Server 2005 or higher to use the approach described in this article, but a similar method can be used for earlier versions.

AminM
- 1,658
- 4
- 32
- 48

Jon Onstott
- 13,499
- 16
- 80
- 133