Find all tables containing column with specified name - MS SQL Server
For a fast result, identifying tables with the input column name, check out the following SQL snippet:
Do not forget to replace %myColumn%
with your desired column name. Above one-liner is your comrade-in-arms for efficiency, and that get-up-and-go solution we all need at times.
Fast answer
Enriching the search with schema details
In some cases, a bit of extra information never hurts. If you're on a quest for more than just table names, enlist schema details to the rescue:
Going broad: Including views with INFORMATION_SCHEMA
For exploring both tables and views, your trusted companion is INFORMATION_SCHEMA.COLUMNS
:
Remember, this cross-DBMS method plays nice with different SQL databases too. A versatile tool in your SQL utility belt.
Deep Dive into INFORMATION_SCHEMA
The craft of detailed exploration
Tailor your queries to bring forth more architecture-specific attributes, like data types or non-null requirements:
Picking your database toolbox
While sys.tables
and sys.columns
are SQL Server's home ground, INFORMATION_SCHEMA
presents a one-size-fits-all solution kind of a Sonic Screwdriver for the Whovians among us.
Unleashing the metadata power of system views
To get your hands on revelatory metadata like modification dates or identity status, unlock the capabilities of SQL Server's system views:
Non-nullable columns: For when NULL just won't do
Set eyes on mandatory data fields by fish-netting those non-null columns:
Answer Upgrade: Beyond the basics
Pinpoint precision with exact matches
Perhaps you want to search for exact names instead of patterns. Fear not, for we have a solution!
Exploring the schema landscape
Extend the radar to scan across sundry schemas by roping in sys.schemas
in your investigation:
Prioritizing your search results
Sometimes, all columns aren't equal, and you want to bring specific ones to the top of your list. Enter, CASE
statements:
Was this article helpful?