Search for a column name in any table

Most directly, you can run this in SQL Server to confirm that a column exists — but that alone is only somewhat useful.

SELECT OBJECT_NAME(object_id) FROM sys.columns WHERE name = 'post_author';

Really what you want to know is both that the column exists and the table it lives in. So here’s a better alternative that joins sys.columns and sys.tables.

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

Code guy.

Articles: 18

Leave a Reply

Your email address will not be published. Required fields are marked *