Monday, January 30, 2012

Find the table to which a given column belongs - Sql Server

[code]
SELECT * FROM sys.columns WHERE NAME LIKE '%ColumnName%'
[/code]
Result Set will contain object_ids in which the "ColumnName" exists. Use following queries to get the corresponding object names.
[code]
SELECT * FROM sys.objects WHERE object_id in (39879409,
729261853,
2044026513
)
[/code]

More civilized code goes here;)

[code]
SELECT * FROM sys.objects O
INNER JOIN sys.columns C
ON O.object_id = C.object_id
WHERE C.NAME LIKE '%ColumnName%'

[/code]

No comments: