Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Finding All tables where a data element is stored

Author  Topic 

btamulis
Yak Posting Veteran

64 Posts

Posted - 2008-08-27 : 17:44:02
Greetings....Is there a script available to search a database for all tables that store a specific value?

Example - 30 tables have a column - ITEMNMBR. What a need to know is how many of the 30 tables store a specific value - let's say ITEMNMBR = 'FINISHED GOOD A'.

I have a script that will list all tables with a column of ITEMNMBR -now I need to know a specific Item Number.

Any thoughts?

Thanks in advance, this forum is usually pretty helpful for me.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-27 : 17:50:47
Run this to generate the queries you'll need to run:
SELECT 'SELECT * FROM ' + TABLE_NAME + ' WHERE ITEMNMBR = ''FINISHED GOOD A'''
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'ITEMNMBR'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

btamulis
Yak Posting Veteran

64 Posts

Posted - 2008-08-27 : 18:07:45
Fantastic......this gives me exactly what I was looking for.....

Absolutely brilliant!

The learning point is that the information_schema.columns is used by SQL to keep track of all the tables with ITEMNMBR column? Correct?

Thanks again....
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-27 : 18:10:07
There are a bunch of INFORMATION_SCHEMA views to query for the schema. You should check them out in the master database and read up on them in SQL Server Books Online.

I typically use TABLES and COLUMNS from those views.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -