Code Snippet - Use T-SQL to Search Table Name by Column Name

2015/12/311 min read
bookmark this
Responsive image

Table of Contents

Introduction

Sometimes you know a column name but don't know which table it belongs to. This SQL example shows how to search for a table name by column name.

Search Table Name by Column Name

SELECT      t.name AS 'TableName', c.name  AS 'ColumnName'
FROM        sys.columns c
JOIN        sys.tables  t   ON c.object_id = t.object_id
WHERE       c.name LIKE '%MyColumn_DontKnowWhichTableIs%'
ORDER BY    TableName
            ,ColumnName;

Conclusion

By querying sys.columns and sys.tables, you can quickly find which tables contain a specific column name. This is especially useful when working with large databases where you don't know the full schema.