Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to find all tables containing column with specified name in MS SQL ?

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

LIKE '%myName%'

?
by

2 Answers

Sonali7
The below code can be implemented to find all tables containing column with specified name in MS SQL :
SELECT      COLUMN_NAME AS 'ColumnName'
,TABLE_NAME AS 'TableName'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%MyName%'
ORDER BY TableName
,ColumnName;
Shahlar1vxp
If you wish to get all tables in MS SQL, the code is as follows-
SELECT TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME like '%'
and TABLE_SCHEMA = 'tresbu_lk'

And if you wish to get all tables with specific column names in MS SQL, then go for the following code-
SELECT DISTINCT TABLE_NAME, COLUMN_NAME  
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name LIKE '%'
AND TABLE_SCHEMA='tresbu_lk'

You may use this query code to replace the desired column name by your own column name-
SELECT TABLE_NAME FROM information_schema.columns WHERE column_name = 'desired_column_name';

Login / Signup to Answer the Question.