One question came up just a day ago while I was writing SQL SERVER – 2005 – Difference Between INTERSECT and INNER JOIN – INTERSECT vs. INNER JOIN.
How many tables in database AdventureWorks have column name like ‘EmployeeID’?
It was quite an interesting question and I thought if there are scripts which can do this would be great. I quickly wrote down following script which will go return all the tables containing specific column along with their schema name.
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;
In above query replace EmployeeID with any other column name.
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;
If you want to find all the column name from your database run following script. You can down any condition in WHERE clause to get desired result.
Reference : Pinal Dave (https://blog.sqlauthority.com)
191 Comments. Leave new
how can i check all tables of a particular database for identifying a unique(name.id) combination and determine the name of this table so that later on i can update the value of name to another value.
can you provide an example Swetha?
How can I search a value in a particular table without knowing column name? Would it be possible to get table name, column name and schema name it is in?
Hello Pinal, is there any way to use sql queries to determine possible relationships between databases ( I know you cannot enforce key constraints between databases). probably querying the statistics object of both databases and matching column data between databases?
Hi i am looking all blank column from all tables of a server.
Please help.
I need to extract all blank column from all tables of a single sql database..
Hi Pinal,
Noticed a small correction. in the order by, it should be t.name instead of t.table_name.
No need. In ORDER BY Clause alias name will work perfectly
Thanks….it helped me.
Hi Pinal,
Great blog. I visit here often through google looking for answers.
This time, I couldnt find one…
I am looking for a specific column name across all tables and all databases in SQL Server.
Can you please help!?
You can use logic given in my blog along with undocumented procedure sp_msforeachdb
That helped. Thank you.
Hi Pinal,
now how do i rename this column from all the tables?
Hi Pinal
How can i get all blank column list from particular table with single query
Hello pinal sir,
i fetch table name from another table column and store table in variable.
eg.
declare @TableName nvarchar(50);
select @TableName=Table_Name from TableRelation where id=1;
print @TableName;
how to select table base on table name which are assign in @TableName;
You need to use Dynamic SQL which is not recommended in this case
select @TableName=’SELECT * FROM +@TableName;
EXEC(TableName);
Great Script. Saved me lots of time today. Thank you old friend.
Search all over for this kind of script. Yours is the only one that works. Thanks!
Glad to hear that Laurie.
Hello, Have you ever do this in a linked server? discard previous comment
Not done with linked server but should work as long as you use OPERNQUERY instead of four part naming.
Thanks a lot !! I always get answers from your posts .
Thanks Lily.
Find all tables containing column with specified name within linked server
Find all tables containing column with specified name in linked server of sqlserver
Thanks for this. Very useful.
Hi, Im trying to run this query on multiple servers but still getting error ” invalid column_name” any suggestions, if thes query can work with sp_MSforeachdb