How to write a query to find all tables in a db that have a specific column name

Using information_schema

This is the standards-compliant cross-RDBMS way to do it.

SELECT table_catalog, table_schema, table_name, column_name
FROM INFORMATION_SCHEMA.columns
WHERE column_name = '<your column name>';

You can see this documented

  • PostgreSQL
  • SQL Server
  • MySQL 8.0

For IBM DB2 you would use the following:

select tabschema,tabname from syscat.columns where colname = 'COLUMN_NAME'

Note that in DB2, column names will be in upper case unless they were defined inside of double quotes with something other than upper case. Then you have to supply the exact casing of the column name as well.


The below query should give you what you're looking for:

use YourDatabase;
go

select
    object_schema_name(t.object_id) + '.' + t.name as table_name,
    c.name as column_name
from sys.tables t
inner join sys.columns c
on t.object_id = c.object_id
where c.name like '%ColumnSearchText%';

If you're looking for columns of an exact name, just replace the WHERE clause with:

where c.name = 'ColumnSearchText';

Tags:

Metadata