samc technologies blogs

SAMC Technologies staff feel it's important to share our experiences with others as part of our open communication approach.  Please feel free to send us any comments you have regarding our blogs. 

SQL: Find all instances of a column name in a database

I have often gone into situations where companies have an undocumented DB scehma. In many cases I have to start with something as basic as their item / customer master. From there I can usually get a good list of field names to begin my search for data to cube. Once I have that I need to know which table to join to, that is which of the tables in the DB have fields that match those I have identified as useful. This is a useful query to find the specified column in all tables in a particular database.

SELECT
 name AS TableName
,CASE WHEN xtype = 'U' THEN 'Table' ELSE 'View' END AS Type
FROM sysobjects
WHERE id IN ( SELECT id FROM syscolumns WHERE name = 'columnname')
AND xtype = 'U'
ORDER BY name