Searching in SQL Management Studio 2005

前端 未结 5 1485
逝去的感伤
逝去的感伤 2021-01-16 09:11

Is there a way to search for text within stored procedures? For example I want to find out if a particular table is being referenced by any stored procedure.

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-16 09:35

    My co-worker graciously provided me with this one recently. It does some similar searching as others have noted, but with a little more added in.

    DECLARE
     @chvStringToFind varchar(256), /* String to find */
     @chrObjectType char(2),--=null, /* Only look for objects of this type */
     @intNbCharToExtract int
     --=50 /* Number of characters to extract before and after the string to find */
     --exec DBA_FindStringInDB @chvStringToFind='sp_helpdia', @chrObjectType=null, @intNbCharToExtract=50
     SET @chvStringToFind = 'EnterSearchTextHere'  -- Change this to search
     SET @chrObjectType = NULL
     SET @intNbCharToExtract = 50
    
     SELECT t.Name, t.TypeDescription, t.CreationDate, t.ModificationDate,
     '...' + SUBSTRING
     (
     t.ObjectDefinition,
     CHARINDEX(@chvStringToFind, t.ObjectDefinition) - @intNbCharToExtract,
     LEN(@chvStringToFind) + (@intNbCharToExtract*2)
     ) + '...' AS Extract
     FROM
     (
     SELECT o.name AS Name, 
     o.type_desc AS TypeDescription, 
     o.create_date AS CreationDate, o.modify_date AS ModificationDate,
     OBJECT_DEFINITION(object_id) AS ObjectDefinition
     FROM sys.objects o
     WHERE 
     ((o.type IN ('AF', 'FN', 'IF', 'P', 'TF', 'TT', 'U', 'V', 'X') AND @chrObjectType IS NULL) OR o.type = @chrObjectType)
     AND OBJECT_DEFINITION(o.object_id) LIKE '%' + @chvStringToFind + '%'
     ) AS t
     ORDER BY TypeDescription, Name
    

提交回复
热议问题