Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all tables that have X column name

Tags:

sql

sql-server

Is there a way to find all the tables that have an X column name within the Y database?

So

If X.Column Exists in Y.Database Print all.tables with x.column

Thanks

like image 260
Angie Avatar asked Jan 10 '23 12:01

Angie


1 Answers

Most, but not all, databases support the information_schema tables. If so, you can do:

select table_name
from information_schema.columns t
where column_name = YOURCOLUMNNAME;

If your database doesn't support the information_schema views, then any reasonable database has an alternative method for getting this information.

You may need to specify the database name, but that depends on the database. It could be:

select table_name
from YOURDATABASENAME.information_schema.columns t
where column_name = YOURCOLUMNNAME;

or

select table_name
from YOURDATABASENAME.information_schema.columns t
where column_name = YOURCOLUMNNAME and schema_name = YOURDATABASENAME;
like image 194
Gordon Linoff Avatar answered Jan 22 '23 20:01

Gordon Linoff