A Query to identify specific fields in SQL Server?

Is there a way to write a query that will identify specific Columns that reside in more than one Table? i.e. Primary Keys, Foreign Keys, etc.

Thanks,
BlakeMcKennaAsked:
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Give this a whirl.  Replace 'id' with whatever column name you wish to search for.
-- This gives you the column names that are used in more than one table
SELECT c.name as column_name, COUNT(c.name) as column_count
FROM sys.tables t
	JOIN sys.columns c ON t.object_id = c.object_id
WHERE c.name = 'id'
GROUP BY C.name

-- This gives you the table names
SELECT c.name as column_name, t.name as table_name
FROM sys.tables t
	JOIN sys.columns c ON t.object_id = c.object_id
WHERE c.name = 'id'
ORDER BY c.name, t.name

Open in new window

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
BlakeMcKennaAuthor Commented:
That's what I was looking for!

Thanks Jim!
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your work.  -Jim
0
BlakeMcKennaAuthor Commented:
Your welcome Jim and thanks again!

God Bless
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 2008

From novice to tech pro — start learning today.