Find the table given a column name

I am using the below query pretty often nowadays, as i was digging my way,analyzing something in a legacy application.
You can replace the COLUMN_NAME in between the %% with the value that you want to search for.

SELECT T.NAME AS TABLE_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 ‘%COLUMN_NAME%’

I wanted to extend it to various databases as well and I found a cool little sp called sp_MSForEachDB .But couldnt get it to work 😦 with my above query.

Advertisements

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s