SQLTeam.com | Weblogs | Forums

Find any particular column among multipel tables


Suppose I have 4 tables with unique column names. So is it possible to find any particular column among these 4 tables.


If you want to find which Table a given Column name is in then:

SELECT	S.name + '.' + T.name + '.' + C.name
FROM	sys.columns AS C
	JOIN sys.tables AS T
		 ON T.object_id = C.object_id
	JOIN sys.schemas AS S
		 ON S.schema_id = T.schema_id
WHERE	C.name = 'YourColumnName'

If you mean that you want to query the data, based on a column name, then I'd need to understand the requirement better, but you would probably need dynamic SQL


Nitpick change:

SELECT	S.name + '.' + T.name + '.' + C.name


That's not a nitpick - that's both an Bugifx and an Upgrade !!