Try this one:
EXEC sp_MSForEachDB 'USE [?]; exec [Database-1_A]..Search_Str
Main Topics
Browse All TopicsI would like to loop through ALL of our SQL databases, searching for a particular text string. All of our databases are named with the format Database-1_A, Database-2_A, for example. We have a Search procedure, called dbo.Search_String_1, which searches for a particular text string within a single database.
When I use the following statement, it seems to loop through the same database (the database in which the search procedure resides) and return results from that same database. Then, after a second or so, the following error is displayed.
Msg 911, Level 16, State 1, Line 1
Database 'Database' does not exist. Make sure that the name is entered correctly.
exec Sp_MSforeachdb 'use ? exec [Database-1_A]..Search_Str
Is there a way to have the procedure, which is not stored in the Master database, to loop through all databases to find a particular text string?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
x-men,
The SP exists within each of the databases. Yet, when I leave off the [Database-1_A].., as you suggest, I get the following error:
Msg 2812, Level 16, State 62, Line 1
Could not find stored procedure 'Search_String_1'.
Also, it seems to loop through 6 times, when there are actually 50+ databases.
Make notice that there is no reference of the DB name, thats why it will run the sp located on the current DB ( [?] )
exec Sp_MSforeachdb 'use ? exec Search_String_1 ''invoice'' '
To run on specific DBs, youl need to load a temporary table with the DB names, and then, use a cursor through the temp table to run the sp
Business Accounts
Answer for Membership
by: x-menPosted on 2009-10-22 at 06:13:37ID: 25633721
copy the SP to each of the DBs, and then:
exec Sp_MSforeachdb 'use ? exec Search_String_1 ''invoice'' '