Search All Stored Procedures in a Database

  • Comments posted to this topic are about the item Search All Stored Procedures in a Database

  • Unfortunately this doesn't quite work, see the explanation in the following article:

    http://www.sqlservercentral.com/articles/Stored+Procedure/62975/

  • I use the following piece of code for a while now and with great success:

    SELECT ROUTINE_NAME, ROUTINE_DEFINITION ROUTINE_TYPE FROM INFORMATION_SCHEMA.ROUTINES

    WHERE ROUTINE_DEFINITION LIKE '%text%' AND ROUTINE_TYPE = 'PROCEDURE'

    http://databases.aspfaq.com/database/how-do-i-find-a-stored-procedure-containing-text.html

  • This still suffers from the same problem, in that the ROUTINE_DEFINITION column is only 4000 characters long. Any stored proc longer than 4000 characters in length is split over two rows and any text that spans this 'split' in the definition will not be found by your method. See the link posted earlier.

  • A slight correction :). Having looked at this further it seems that the INFORMATION_SCHEMA.ROUTINES view does not split the procedure definition over more than 1 row it in fact only shows the first 4000 characters of any stored proc. Which actually makes using this view for searching the procedure definitinion less than useful.

    Oh, and the INFORMATION_SCHEMA views are only in 2005 and above.

    For 2005 and above the take a look at the OBJECT_DEFINITION function, mentinoed in the following article on the same subject:

    http://databases.aspfaq.com/database/how-do-i-find-a-stored-procedure-containing-text.html

  • Assuming this post belongs to SQL server 2005, you can use the below query to return all SP in the database

    SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'PROCEDURE'

    Abhijit - http://abhijitmore.wordpress.com

  • I added a quick option to George's original block to specify the DB you want to search. This way it can sit in Master and be called from anywhere on the server.

    But this split at character 4000 in syscomments is an eye opener. Means this solution isn't complete, even for searching procs only.

    Ken

    ALTER proc [dbo].[SearchObjects]

    @database varchar(100),

    @searchString varchar(100)

    As

    Declare @sql nvarchar(1000)

    Set @sql = 'SELECT Distinct SO.NameFROM ' + @database + '.dbo.' + 'sysobjects SO (NOLOCK) ' +

    'INNER JOIN ' + @database + '.dbo.' + 'syscomments SC (NOLOCK) on SO.Id = SC.ID ' +

    'AND SO.Type = ''P'' AND SC.Text LIKE ''%' + @searchString + '%'' ORDER BY SO.Name'

    Exec sp_executesql @sql

  • One suggestion is to add the upper to both side of the comparision to make it work with case senstive databases.

    Ex: where upper(text) like upper(@searchstring)

  • Thanks for the script.

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply