SQL Query to find the column name in all the tables in a database

The below query will display the list of all the tables with same column name:

USE <DatabaseBase>
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 AS C ON T.OBJECT_ID = C.OBJECT_ID
WHERE C.name LIKE ‘%<ColumnName>%’
ORDER BY schema_name, Table_Name;

Advertisements

2 Responses to SQL Query to find the column name in all the tables in a database

  1. madhivanan says:

    It is good practice to avoid querying system tables/views. You can very well use information_schema.columns. This is simple

    select table_name,table_schema,column_name from INFORMATION_SCHEMA.COLUMNS
    where COLUMN_NAME like ‘%id%’

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: