Find all tables containing column with specified name - MS SQL Server

gruber picture gruber · Jan 31, 2011 · Viewed 2.4M times · Source

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

LIKE '%myName%'

?

Answer

AdaTheDev picture AdaTheDev · Jan 31, 2011

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 & Views:

SELECT      COLUMN_NAME AS 'ColumnName'
            ,TABLE_NAME AS  'TableName'
FROM        INFORMATION_SCHEMA.COLUMNS
WHERE       COLUMN_NAME LIKE '%MyName%'
ORDER BY    TableName
            ,ColumnName;