Solved

using mast db to get table names using column names

Posted on 2012-03-16
7
375 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
ID: 37729395
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:
Scott Pletcher earned 167 total points
ID: 37729399
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
ID: 37729416
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 25

Expert Comment

by:Lee Savidge
ID: 37729428
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
ID: 37729438
Obviously your where clause can be whatever you need

where C.name = 'column_name'
0
 

Author Closing Comment

by:ac_davis2002
ID: 37730106
thanks!!
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 37730121
thanks
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

837 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