Search for text in Stored Procs across many databases?

Hi -

I want to find a table name in stored procs that can reside in several databases.

I have code to find the table name in a single database but I'd want to search across all of the databases on the server.

Thanks,
Mark
MarkMahonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

viralypatelCommented:
This query will return the names of all the stored procedures in the current database containing the text you want to search.
select object_name(id) from syscomments where text like '%Text you want to search%'

To search across multiple databases add the database name before the syscomments table and write separate queries for each database inside a stored procedure and pass the search string as parameter to it.


CREATE PROC sp_SelectFromTable @searchStr VARCHAR (128)
AS
EXEC ('select object_name(id) from ' + 'database1' + '.syscomments where text like ' + @searchStr )
EXEC ('select object_name(id) from ' + 'database2' + '.syscomments where text like ' + @searchStr )
EXEC ('select object_name(id) from ' + 'database3' + '.syscomments where text like ' + @searchStr )
EXEC ('select object_name(id) from ' + 'database4' + '.syscomments where text like ' + @searchStr )
--add as many databases as you want here.
GO

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
viralypatelCommented:
changing the name of the sp:

CREATE PROC sp_SearchAcrossDBs @searchStr VARCHAR (128)
AS
EXEC ('select object_name(id) from ' + 'database1' + '.syscomments where text like ' + @searchStr )
EXEC ('select object_name(id) from ' + 'database2' + '.syscomments where text like ' + @searchStr )
EXEC ('select object_name(id) from ' + 'database3' + '.syscomments where text like ' + @searchStr )
EXEC ('select object_name(id) from ' + 'database4' + '.syscomments where text like ' + @searchStr )
--add as many databases as you want here.
GO

Open in new window

0
Éric MoreauSenior .Net ConsultantCommented:
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

aprilgalCommented:
If you have access to TOAD (Tool for Oracle Developers) this search is very easy.  First do an object search and select all objects you want to search (ie, packages, procs, functions, tables, triggers, etc).  Enter what you are searching for and GO.  Then you can repeat the same action on a different session by clicking the little blue button in the upper left corner.
0
MarkMahonAuthor Commented:
Thanks for all of the comments.  

Unfortunately I don't have TOAD.  The free software is looks promising if I can't get the first code to work.

Question for viralpatel ... does "syscomments" cover views and functions?  To be on the safe side I should look at those too.

Thanks again for the comments.

Mark
0
viralypatelCommented:
yes they work for sure on views, i guess on functions too ... just give it a try with one statement.
0
MarkMahonAuthor Commented:
Thanks for the help.
0
Anthony PerkinsCommented:
Red-Gate's SQL Search is the way to go.  But if you want to do it programatically than this should do it (fix the obvious typo in xDECLARE):

EXEC sp_msforeachdb '
USE [?]
xDECLARE @SearchString sysname = ''YourTableNameGoesHere''
If EXISTS (
    SELECT  1
    FROM    sys.procedures
    WHERE   CHARINDEX(@SearchString, OBJECT_DEFINITION(object_id)) > 0
    )
    SELECT  DB_NAME(), NAME
    FROM    sys.procedures
    WHERE   CHARINDEX(@SearchString, OBJECT_DEFINITION(object_id)) > 0
'

Open in new window

0
MarkMahonAuthor Commented:
Thanks AC ... Redgate looks really nice
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.