Click here to Skip to main content
15,885,365 members
Articles / Database Development / SQL Server
Alternative
Tip/Trick

Find all Stored Procedures having a given text in it

Rate me:
Please Sign up or sign in to vote.
4.86/5 (4 votes)
11 Oct 2011CPOL 8.7K   2   2
We use a slightly modified approach that returns results from more than stored procedures (although it could be modified to return only results for stored procedures). We use this code in a stored procedure that requires a parameter containing the text to search for.@StringToFind...
We use a slightly modified approach that returns results from more than stored procedures (although it could be modified to return only results for stored procedures). We use this code in a stored procedure that requires a parameter containing the text to search for.

SQL
@StringToFind VarChar(7998)

AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @LikeSearch VarChar(8000)
    SET @LikeSearch = '%' + @StringToFind + '%'

    SELECT Distinct SO.Name
        ,CASE SO.Type
            WHEN 'C' THEN 'CHECK constraint'
            WHEN 'D' THEN 'Default or DEFAULT constraint'
            WHEN 'F' THEN 'FOREIGN KEY constraint'
            WHEN 'FN' THEN 'Scalar function'
            WHEN 'IF' THEN 'In-lined table-function'
            WHEN 'K' THEN 'PRIMARY KEY or UNIQUE constraint'
            WHEN 'L' THEN 'Log'
            WHEN 'P' THEN 'Stored procedure'
            WHEN 'R' THEN 'Rule'
            WHEN 'RF' THEN 'Replication filter stored procedure'
            WHEN 'S' THEN 'System table'
            WHEN 'TF' THEN 'Table function'
            WHEN 'TR' THEN 'Trigger'
            WHEN 'U' THEN 'User table'
            WHEN 'V' THEN 'View'
            WHEN 'X' THEN 'Extended stored procedure'
            ELSE 'Unidentified' END AS Type
        ,@StringToFind AS SearchedFor
        ,Definition AS SearchedIn
        ,CHARINDEX(@StringToFind, definition, 0) AS PositionWhereFound
        ,LEN(definition) AS TextLength
    FROM sysobjects SO (NOLOCK)
       INNER JOIN sys.sql_modules SM (NOLOCK) on SO.Id = SM.object_id
       AND SM.Definition LIKE @LikeSearch
    ORDER BY Type,SO.Name

END



Why the 7998 size designation? VarChar has a limit of 8000 characters and we need to add '%' to both the front and back of the string for the LIKE functionality to work.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralJust one comment, as sql_modules will have row's only for Ob... Pin
Basavaraj P Biradar18-Nov-11 20:18
Basavaraj P Biradar18-Nov-11 20:18 
GeneralReason for my vote of 5 Great script! Pin
VMAtm18-Oct-11 1:37
VMAtm18-Oct-11 1:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.