brown wooden drawer

Where is That Column in Microsoft SQL?

I was writing a SQL query to pull data from an existing vendor database into a report. Instead of just ‘dbo.users’ having an ‘id’ column, they normally named the primary key ‘user_id’. While we can argue about the semantics, it had a nice effect that I could search for the column name across the entire database and have pretty good confidence that if it’s in another table, it’s a reference key to the primary table.
If you’re in Microsoft SQL server and need to figure out if a column in one table is relisted elsewhere, use the following SQL script:
SELECT      c.name  AS 'ColumnName'
            ,t.name AS 'TableName'
FROM        sys.columns c
JOIN        sys.tables  t   ON c.object_id = t.object_id
WHERE       c.name LIKE '%MyName%'
ORDER BY    TableName
            ,ColumnName;
To search for a column name in both tables and views in Microsoft SQL server:
SELECT      COLUMN_NAME AS 'ColumnName'
            ,TABLE_NAME AS  'TableName'
FROM        INFORMATION_SCHEMA.COLUMNS
WHERE       COLUMN_NAME LIKE '%MyName%'
ORDER BY    TableName
            ,ColumnName;
Thanks to @adathedev and @aeyjey for the StackOverflow post.