1

I have a large database with over 100 tables running on MS SQL Server 2012.

I have a series of column names in an output (Excel file) which was created in the past by another person.

How can I find to which SQL tables the column names belong to?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lorenzo Rigamonti
  • 1,705
  • 8
  • 25
  • 36

3 Answers3

1

Use this

SELECT  distinct table_name FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name='your_column_name'
Madhivanan
  • 13,470
  • 1
  • 24
  • 29
0

You can use the below query if you have the column name you are looking for.

SELECT sc.name,so.name FROM syscolumns sc JOIN 
 sys.tables st ON sc.id = st.object_id WHERE st.type = 'U' and sc.name = 'Your_column_name'

Of course there are additional filters that you can explore on these tables to filter the type of objects down.

Kashyap MNVL
  • 603
  • 4
  • 13
0

If you need to get current list of tables/cols and then compare with old document:

SELECT 
     [st].[name]    AS "@name"
    ,[sc].[columns] AS "*"    
FROM 
    [sys].[tables] AS [st] 
OUTER APPLY
    (
        SELECT [columns] = CAST((SELECT [name] as "column/@name" FROM [sys].[columns] WHERE [object_id] = [st].[object_id] FOR XML PATH('') ) AS XML)
    ) AS [sc]
WHERE  [st].[type] = 'U'            
FOR XML PATH('table'), ROOT('root');
Juozas
  • 916
  • 10
  • 17