Pages

Tuesday, April 2, 2013

How to find table names which have a specific column name in SQL Server?

In SQL Server Database, if you want to finds all the table names which have a specific column name field, you can use the JOIN query with "sys.tables" and "sys.columns" system tables. For example, if you want to find all the tables which have the column name "ITEM" use the following query,
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 '%ITEM%' ORDER BY schema_name, table_name;