Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

LIKE '%myName%'

?

like image 439
gruber Avatar asked Jan 31 '11 10:01

gruber


People also ask

How do I find a column name in SQL Server Management Studio?

Column search This will open the Object search panel within SSMS for searching all SQL Server objects by entering a search phrase and clicking the Find button or hitting Enter from the keyboard. This action will ensure that only columns will be included in the search.


3 Answers

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

SELECT      COLUMN_NAME AS 'ColumnName'
            ,TABLE_NAME AS  'TableName'
FROM        INFORMATION_SCHEMA.COLUMNS
WHERE       COLUMN_NAME LIKE '%MyName%'
ORDER BY    TableName
            ,ColumnName;
like image 129
AdaTheDev Avatar answered Oct 22 '22 17:10

AdaTheDev


We can also use the following syntax:-

select * from INFORMATION_SCHEMA.COLUMNS 
where COLUMN_NAME like '%clientid%' 
order by TABLE_NAME
like image 468
Khwaza Bandenawaz Avatar answered Oct 22 '22 16:10

Khwaza Bandenawaz


SQL Server:

SELECT Table_Name, Column_Name 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = 'YOUR_DATABASE'
AND COLUMN_NAME LIKE '%YOUR_COLUMN%'

Oracle:

SELECT owner, table_name, column_name 
FROM all_tab_columns 
WHERE column_name LIKE '%YOUR_COLUMN_NAME%'
AND OWNER IN ('YOUR_SCHEMA_NAME');
  • SIMPLE AS THAT!! (SQL, PL/SQL)
    I use it ALL the time to find ALL instances of a column name in a given database (schema).
like image 230
Todd-Phreesia Avatar answered Oct 22 '22 17:10

Todd-Phreesia