Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching one value in all the tables in one Database 5

Status
Not open for further replies.

redpotato

Programmer
Feb 15, 2010
24
CA
Hello guys,

maybe it sounds crazy but I would like to search one value which might exist in certain tables but there are like thousands tables exist in a DB.
how could I search that in one shot?

one more thing, how can I get all table names in one DB with Stored Procedure?

Thanks for your help!
 
There is an undocumented procedure that should work for you.
Code:
exec sp_MSForEachTable
 @command1 = 'command'

Example:
Code:
exec sp_MSForEachTable
  @command = 'SELECT lname FROM ? WHERE city = ''Pittsburgh'''

-SQLBill

The following is part of my signature block and is only intended to be informational.
Posting advice: FAQ481-4875
 
Thanks for the help.
but when I ran the command below, it gives me Could not find stored procedure 'sp_MSForEachTable'

exec sp_MSForEachTable
@command = 'SELECT item FROM ? WHERE item = ''58I'''

did you mean to create sp_MSForEachTable stored procedure?
 
sorry, i found out how to run the sp.
but now i get this message below,

Server: Msg 8145, Level 16, State 2, Procedure sp_MSforeachtable, Line 0
@command is not a parameter for procedure sp_MSforeachtable.
 
Try this:
Code:
EXEC sp_msForEachTable 'SELECT * FROM ?  WHERE item = ''58I'' '
 
I left off the number 1 in my example...it has to be:

@command1

-SQLBill

The following is part of my signature block and is only intended to be informational.
Posting advice: FAQ481-4875
 
thanks guys,

I tried jbenson001's command and it works!
and SQLBill's command worked as well!!

Thank you guys!!
 
jbenson001's worked since there was only one command. I should have included more information...but if you ever need to use it again, you can include up to three commands by using:
@command1 =
@command2 =
@command3 =
There are some other options also, you should be able to find them via a search.

You can also do the same thing for every database with: sp_MSForEachDB

-SQLBill

The following is part of my signature block and is only intended to be informational.
Posting advice: FAQ481-4875
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top