Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

Find particular word or text from the entire stored procedure

0.00/5 (No votes)
26 Jan 2012CPOL 5.3K  
since syscomments is nvarchar(4000) the word may be split into several rows.. so you concat them.. or at least pair them up... here is the sp I use to search on the stored proceduresCREATE PROCEDURE [dbo].[sp_FindInProc] @sText NVARCHAR(MAX) = '%'ASBEGIN SET NOCOUNT ON ...
since syscomments is nvarchar(4000) the word may be split into several rows.. so you concat them.. or at least pair them up... here is the sp I use to search on the stored procedures

SQL
CREATE PROCEDURE [dbo].[sp_FindInProc]
    @sText NVARCHAR(MAX) = '%'
AS
BEGIN

    SET NOCOUNT ON
    
SELECT DISTINCT obj.id as ID,obj.name AS Name
FROM
(
            SELECT  a.id,CAST(COALESCE(a.text,'') AS NVARCHAR(MAX)) + CAST(COALESCE(b.text,'')  AS NVARCHAR(MAX)) AS [text]
            FROM    syscomments a LEFT JOIN 
                    syscomments b ON a.colid+1 = b.colid and a.id = b.id
) AS comment
INNER JOIN sysObjects obj ON obj.id = comment.id
WHERE obj.type = 'P' AND comment.text like @sText
ORDER BY obj.Name            

    SET NOCOUNT OFF

END

License

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