How to search all tables for a common column name

I need a sql query that will return a list of all of the tables which have a common column name. For example if I have a column named EmployeeId, I want a sql query that will return all of the tables which have the "EmployeeId" column.
LVL 2
brgdotnetcontractorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark BullockQA Engineer IIICommented:
SELECT table_name from information_schema.columns where column_name='EmployeeId';
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperCommented:
SELECT distinct column_name, table_name from information_schema.columns

Open in new window

0
Mike EghtebasDatabase and Application DeveloperCommented:
This may be also handy to have:
SELECT distinct column_name, 
   (
      SELECT table_name + '  '
      FROM information_schema.columns
	  WHERE column_name = A.column_name
      FOR XML PATH('')
   ) AS Notes
FROM information_schema.columns AS A

Open in new window


It returns:
column_name    table_names
address	        Employees  Suppliers  Customers  
birthdate       Employees  
categoryid	Categories  Products  RankedProducts  
categoryname	Categories  
city	        Employees  Suppliers  Customers  
companyname	Suppliers  Customers  Shippers  
contactname	Suppliers  Customers  
contacttitle	Suppliers  Customers  
country	        Employees  Suppliers  Customers  
custid	        Customers  Orders  OrderValues  CustOrders  
description	Categories  
discontinued Products  
discount	 OrderDetails  
empid	       Employees  Orders  OrderValues  EmpOrders  
fax              Suppliers  Customers  
firstname	Employees  
freight	       Orders  
hiredate	   Employees  
lastname	Employees  
mgrid	        Employees  
n	               Nums  
numorders	EmpOrders  
orderdate	Orders  OrderValues  
orderid	        Orders  OrderDetails  OrderValues  
ordermonth	CustOrders  EmpOrders  
orderyear	OrderTotalsByYear  
phone	        Employees  Suppliers  Customers  Shippers  
postalcode	Employees  Suppliers  Customers  
productid	Products  OrderDetails  RankedProducts  
productname	Products  RankedProducts  
qty	                 OrderDetails  OrderValues  OrderTotalsByYear  CustOrders  EmpOrders  
region	         Employees  Suppliers  Customers  
requireddate	Orders  OrderValues  
rownum	        RankedProducts  
score	        Scores  
shipaddress	Orders  
shipcity	        Orders  
shipcountry	Orders  
shipname	Orders  
shippeddate	Orders  OrderValues  
shipperid	Shippers  Orders  OrderValues  
shippostalcode	Orders  
shipregion	Orders  
studentid	Scores  
supplierid	Suppliers  Products  
testid	       Tests  Scores  
title	         Employees  
titleofcourtesy	Employees  
unitprice       Products  OrderDetails  RankedProducts  
val	         OrderValues  EmpOrders  

Open in new window

0
Mike EghtebasDatabase and Application DeveloperCommented:
So, for field named 'address'
SELECT distinct column_name, 
   (
      SELECT table_name + '  '
      FROM information_schema.columns
	  WHERE column_name = A.column_name
      FOR XML PATH('')
   ) AS Notes
FROM information_schema.columns AS A
Where column_name='address'

Open in new window


it returns:

column_name    table_names
address                   Employees  Suppliers  Customers  

Mike
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.