Getting table name from column name

Published 15 September 07 05:23 AM | SQL Master 

If you know the name of one column you want to find from which table that is originated, to retrieve the table name you can use following TSQL:

SELECT CASE (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS AS c2 WHERE c2.TABLE_NAME = c1.TABLE_NAME AND c2.COLUMN_NAME < c1.COLUMN_NAME)
WHEN 0 THEN c1.TABLE_NAME
ELSE ''
END AS TABLE_NAME,
c1.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS AS c1
ORDER BY c1.TABLE_NAME,
c1.COLUMN_NAME

I believe using Information_Schema views are best to get optimum values than directly querying the system tables in SQL Server.

 

Comments

# SSQA.net - SqlServer-QA.net said on September 15, 2007 7:40 AM:

If you know the name of one column you want to find from which table that is originated, to retrieve

# Other SQL Server Blogs around the Web said on September 15, 2007 12:17 PM:

If you know the name of one column you want to find from which table that is originated, to retrieve

Anonymous comments are disabled

About SQL Master

**__________________________________** SQL Server MVP, Sr. DBA & industry expert. - Knowledge is of two kinds. We know a subject ourselves or we know where we can find information on it. It is also a power and you will gain by sharing it.

Search

Go

This Blog

«September 2007»
SMTWTFS
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

Syndication