We help IT Professionals succeed at work.

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
Comment
Watch Question

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

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

Éric MoreauSenior .Net Consultant
Top Expert 2016
Commented:

Commented:
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.

Author

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
yes they work for sure on views, i guess on functions too ... just give it a try with one statement.

Author

Commented:
Thanks for the help.
Top Expert 2012

Commented:
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

Author

Commented:
Thanks AC ... Redgate looks really nice