SQL SERVER – Query to Find Column From All Tables of Database

How do you find one column from all tables of the database? How many tables in AdventureWorks have a column called EmployeeID? It came up while I was writing Difference Between INTERSECT and INNER JOIN, and it turned out to be a handy script to have.

USE AdventureWorks
GO
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%EmployeeID%'
ORDER BY schema_name, table_name;

SQL Query2' results and messages window.

In above query replace EmployeeID with any other column name.

If you want to find all the column names from your database, run the same script without the WHERE clause.

SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
ORDER BY schema_name, table_name;

Database query tool window.

Add the data type while you are there

Nine times out of ten, the reason you are hunting for a column across tables is that you are about to change it, or you are wondering why a join is slow. Both questions need the type, not just the name.

SELECT SCHEMA_NAME(t.schema_id) AS SchemaName,
       t.name AS TableName,
       c.name AS ColumnName,
       ty.name AS DataType,
       c.max_length,
       c.is_nullable
FROM sys.tables AS t
INNER JOIN sys.columns AS c ON t.object_id = c.object_id
INNER JOIN sys.types AS ty ON c.user_type_id = ty.user_type_id
WHERE c.name LIKE '%EmployeeID%'
ORDER BY SchemaName, TableName;

Run this across a database you have inherited and you often find the same column defined three different ways: INT in one table, BIGINT in another, VARCHAR in a third. That is usually the reason a join runs badly, and it is very hard to spot any other way.

Views and procedures have columns too

sys.tables only returns tables, which is exactly what the question asked for. But if you are looking for everywhere a column appears, views are part of the answer.

SELECT SCHEMA_NAME(o.schema_id) AS SchemaName,
       o.name AS ObjectName,
       o.type_desc AS ObjectType,
       c.name AS ColumnName
FROM sys.objects AS o
INNER JOIN sys.columns AS c ON o.object_id = c.object_id
WHERE c.name LIKE '%EmployeeID%'
  AND o.type IN ('U', 'V')
ORDER BY o.type_desc, SchemaName, ObjectName;

U is a user table and V is a view. Swapping sys.tables for sys.objects is the only real change.

The standard version

There is an INFORMATION_SCHEMA version of this, and it is worth knowing because it works on other database engines too.

SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%EmployeeID%'
ORDER BY TABLE_SCHEMA, TABLE_NAME;

Use this one if your script has to travel. Use the sys version on SQL Server, because it knows about things the standard views do not, like whether a column is an identity or computed.

Searching every database on the server

Sometimes you do not know which database the column is in. This walks them all.

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql = @sql + N'
SELECT ''' + name + N''' AS DatabaseName,
       SCHEMA_NAME(t.schema_id) AS SchemaName,
       t.name AS TableName, c.name AS ColumnName
FROM ' + QUOTENAME(name) + N'.sys.tables AS t
JOIN ' + QUOTENAME(name) + N'.sys.columns AS c ON t.object_id = c.object_id
WHERE c.name LIKE ''%EmployeeID%'' UNION ALL'
FROM sys.databases
WHERE state = 0 AND database_id > 4;

SET @sql = LEFT(@sql, LEN(@sql) - 9);
EXEC sp_executesql @sql;

Two details make it safe. state = 0 skips databases that are offline or restoring, which is what breaks most scripts like this. database_id > 4 skips master, model, msdb and tempdb, which you almost never want in the results.

One thing to watch with LIKE

Searching for ‘%ID%’ will match EmployeeID, OrderID, and also Identity, Video and Guidance. If you want the column named exactly EmployeeID, drop the wildcards and use c.name = 'EmployeeID'. If you want columns ending in ID, use '%ID' with the wildcard only at the front.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.

SQL Data Storage, SQL Scripts, SQL Stored Procedure, SQL System Table
Previous Post
SQL SERVER – 2005 – Get Field Name and Type of Database Table
Next Post
SQL SERVER – 2008 – Released To Manufacturing Available

Related Posts

191 Comments. Leave new

  • I know only DBMS concepts
    give me tips to know SQL

    Reply
  • Which one is best, * or Specified column names in a Select Query?Why?

    Reply
  • this just saved me from digging through a few thousand lines of
    code. Thanks :)

    Reply
  • this just saved me from digging through a few thousand lines of source code. Thanks.

    Reply
  • Thanks so much! Your blog has been very helpful to me lately! :)

    Reply
  • I just wanted to finally say thanks – every time I google a tsql question, whenever your face appears, I follow the link knowing I’ll get good, solid, reliable advice. You sir, give excellent nuggets and are quite an asset to the internet at large. Thank you.

    Reply
  • Pinal Sir, the 2nd best part of your blog posts is the large no. of comments, through which one can learn different ideas.
    Thanks for providing us such a useful blog !!!

    Reply
  • I have a large database and I want to determine how many tables have a specific column with a specific value in it.

    Reply
  • This works great and I just tried it with date and the only thing I can think of is data type…such as date datetime or smalldatetime
    how would I add a column for the format of the date ?

    Reply
  • Johirul Islam
    May 18, 2012 3:58 pm

    here is the result

    SELECT Table_Schema, Table_Name, Column_Name, Data_Type
    FROM information_schema.columns
    WHERE table_name in ( select name from sys.objects
    where type = ‘U’ )
    and column_name like ‘%EmployeeID%’
    order by table_schema, table_name

    Reply
  • Thanks Johirul that really helped.

    Reply
  • Hi,

    Can someone help me with a script that looks at how many tables in oracle schema that have column name like ‘EmployeeID’?

    Actual I need a very same script that works like the one below but in Oracle aqua, not in ms server.

    SELECT t.name AS table_name,
    SCHEMA_NAME(schema_id) AS schema_name,
    c.name AS column_name
    FROM sys.tables AS t
    INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
    WHERE c.name LIKE ‘%EmployeeID%’
    ORDER BY schema_name, table_name;

    Thanks in advance

    Reply
  • thank you works like a charm

    Reply
  • Kamakshi Suram
    June 12, 2012 1:12 pm

    Hi Dave,

    I have a question.
    Is it possible for you to get data for particulat value.
    For Eg:
    I have a table’ senario1_SRC ‘with 3 columns. col1, col2, col3.

    From the below query i will get ouput as ‘col1′

    select top 1 column_name from information_schema.columns where table_name=’senario1_SRC’

    Next I need the data from above resulted columns.

    Like below
    SELECT
    (select top 1 column_name from information_schema.columns where table_name=’senario1_SRC’)

    FROM senario1_SRC

    If I fire the above query I should get data of the column.

    But I am getting col1 as result

    Can you please help me on above issue.

    Reply
  • Brilliant thank you, I used this to find the junction table for two columns in a very large database I was not familiar with by changing the WHERE clause to:

    WHERE c.name LIKE ‘%FIRSTCOLUMN%’
    OR c.name LIKE ‘%SECONDCOLUMN%’

    Reply
  • thank u sir.

    Reply
  • Hello, I am having problems. I need to bring up two different data. Like for example Title and all the pages less then 250 and only those two how can I do it. The table is called books.

    Reply
  • Is there an equivalent Query for an IBM DB2 database?

    Reply
  • I have several tables in a database, which all start with the name Products and I would like to be able to do a search for all ‘part numbers’, and all ‘descriptions’ (which are both columns contained in all of the databases) and then display all of the information in a single table or view.

    Reply
  • Hello sir,

    If there are 50 columns in a table and i want display only 48 columns , how it can be done without writing the 48 columns in select query. Is there any way to find this? Please reply me. [email removed]

    Reply

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.