How do I find a stored procedure containing ?

后端 未结 20 2087
梦谈多话
梦谈多话 2020-11-28 17:21

I need to search a SQL server 2008 for stored procedures containing where maybe the name of a database field or variable name.

20条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 17:34

    I use this script. If you change your XML Comments to display as black text on a yellow background you get the effect of highlighting the text you're looking for in the xml column of the results. (Tools -> Options -> Environment -> Fonts and Colors [Display items: XML Comment]

        ---------------------------------------------
        --------------   Start  FINDTEXT   ----------
        ---------------------------------------------
    
        SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 
        SET NOCOUNT ON
        GO
        DECLARE @SearchString VARCHAR(MAX) 
        SET @SearchString = 'the text you''re looking for'
        DECLARE @OverrideSearchStringWith VARCHAR(MAX) 
        --#############################################################################
        -- Use Escape chars in Brackets []  like [%] to find percent char.
        --############################################################################# 
    
        DECLARE @ReturnLen INT 
        SET @ReturnLen = 50;
        with    lastrun
                  as (select    DEPS.OBJECT_ID
                               ,MAX(last_execution_time) as LastRun
                      from      sys.dm_exec_procedure_stats DEPS
                      group by  deps.object_id
                     )
            SELECT  OL.Type
                   ,OBJECT_NAME(OL.Obj_ID) AS 'Name'
                   ,LTRIM(RTRIM(REPLACE(SUBSTRING(REPLACE(OBJECT_DEFINITION(OL.Obj_ID), NCHAR(0x001F), ''), CHARINDEX(@SearchString, OBJECT_DEFINITION(OL.Obj_ID)) - @ReturnLen, @ReturnLen * 2), @SearchString, '   ***-->>' + @SearchString + '<<--***  '))) AS SourceLine
                   ,CAST(REPLACE(REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(MAX), REPLACE(OBJECT_DEFINITION(OL.Obj_ID), NCHAR(0x001F), '')), '&', '(A M P)'), '<', '(L T)'), '>', '(G T)'), @SearchString, '' + @SearchString + '<-->') AS XML) AS 'Hilight Search'
                   ,(SELECT [processing-instruction(A)] = REPLACE(OBJECT_DEFINITION(OL.Obj_ID), NCHAR(0x001F), '')
                    FOR
                     XML PATH('')
                        ,TYPE
                    ) AS 'code'
                   ,Modded AS Modified
                   ,LastRun as LastRun
            FROM    (SELECT CASE P.type
                              WHEN 'P' THEN 'Proc'
                              WHEN 'V' THEN 'View'
                              WHEN 'TR' THEN 'Trig'
                              ELSE 'Func'
                            END AS 'Type'
                           ,P.OBJECT_ID AS OBJ_id
                           ,P.modify_Date AS modded
                           ,LastRun.LastRun
                     FROM   sys.Objects P WITH (NOLOCK)
                            LEFT join lastrun on P.object_id = lastrun.object_id
                     WHERE  OBJECT_DEFINITION(p.OBJECT_ID) LIKE '%' + @SearchString + '%'
                            AND type IN ('P', 'V', 'TR', 'FN', 'IF', 'TF')
                         --   AND lastrun.LastRun  IS NOT null
                    ) OL
        OPTION  (FAST 10)
    
        ---------------------------------------------
        ----------------    END     -----------------
        ---------------------------------------------
        ---------------------------------------------
    

提交回复
热议问题