Solved

using mast db to get table names using column names

Posted on 2012-03-16
7
372 Views
Last Modified: 2012-08-13
hi

I have been given the column names i need to use but not the table names they belong to.

can anyone give me a query to search the system tables to get the table names where column names like 'cloumn name'

Thanks
0
Comment
Question by:ac_davis2002
7 Comments
 
LVL 13

Assisted Solution

by:Philip Pinnell
Philip Pinnell earned 167 total points
Comment Utility
I use this when looking for field names


Select
  O.name objectName,
  C.name ColumnName
from sys.columns C

inner join sys.objects O
  ON C.object_id=O.object_id

where 1=1
--and C.name like '%renew%'
and C.name like '%reason%'

order by O.name,C.name
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 167 total points
Comment Utility
This will show you all tables that have ALL the column names you specify:

SELECT o.name
FROM sys.objects o
INNER JOIN sys.columns c ON
    c.object_id = o.object_id
WHERE
    c.name IN ('col_name1', 'col_name2', ...)
GROUP BY
    o.name
HAVING
    COUNT(*) = <number_of_col_names>
0
 
LVL 25

Assisted Solution

by:Lee Savidge
Lee Savidge earned 166 total points
Comment Utility
select t.table_name, c.column_name from information_schema.columns c
join information_schema.tables t
on c.table_name = t.table_name
and table_type = 'base table'
where column_name in ('mycolname', 'mycolname2')
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 25

Expert Comment

by:Lee Savidge
Comment Utility
Obviously you can alter the where clause:

where c.column_name like '%colname%'

where c.column_name = 'mycolname'

etc.
0
 
LVL 13

Expert Comment

by:Philip Pinnell
Comment Utility
Obviously your where clause can be whatever you need

where C.name = 'column_name'
0
 

Author Closing Comment

by:ac_davis2002
Comment Utility
thanks!!
0
 
LVL 13

Expert Comment

by:Philip Pinnell
Comment Utility
thanks
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now