How do I find a stored procedure containing ?

后端 未结 20 2024
梦谈多话
梦谈多话 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:43

    For any SQL server newer than SQL server 2000:

    SELECT object_name = OBJECT_NAME(sm.object_id), o.type_desc, sm.definition  
    FROM sys.sql_modules AS sm  
    JOIN sys.objects AS o ON sm.object_id = o.object_id  
    WHERE sm.definition like '%searchString%'  
    ORDER BY  o.type, o.name, o.object_id
    

    If someone is stuck with SQL server 2000, the table sql_modules doesn't exist, so you would use syscomments, you will get multiple records for stored procdedures larger than 4000 characters, but they will have the same c.number field so you can group the parts together to get the full stored procedure text:

        Select o.id, c.number, o.name, c.text  
        from syscomments c 
        inner join sysobjects o on o.id = c.id 
        where c.encrypted = 0 and o.type = 'P'  
          and c.id in  
         (Select id from syscomments where text like '%searchtext%')
        order by objecttype, o.name, o.id, c.number, c.colid        
    
    0 讨论(0)
  • 2020-11-28 17:45
    SELECT s.name + '.' + o.name ProcedureName
    , c.text ProcedureSteps
    FROM   sys.syscomments c 
    INNER JOIN
    sys.objects o 
    ON 
    c.id = o.object_id
    INNER JOIN
    sys.schemas s 
    ON 
    o.schema_id = s.schema_id
    WHERE  o.type = 'P'
    AND c.text LIKE N'%XXXX%'
    ORDER BY s.name + '.' + o.name
    , c.colid
    

    This query returns the name and the content of any stored procedure where "XXXX" is is referenced within the stored procedure.

    This is quit usefull when finding procedures that reference a specific table/view/procedure

    0 讨论(0)
  • 2020-11-28 17:51
    SELECT OBJECT_NAME(id)     
    FROM syscomments 
    WHERE [text] LIKE '%Name%' 
    AND OBJECTPROPERTY(id, 'IsProcedure') = 1 
    GROUP BY OBJECT_NAME(id)
    

    Try This .....

    0 讨论(0)
  • 2020-11-28 17:52

    Here is the union version that use Losbear made, but modified so that it also outputs the schema name:

    DECLARE @SearchText varchar(1000) = 'searchtext';
    
    SELECT DISTINCT sc.SPECIFIC_SCHEMA as 'Schema', sp.SPName as 'Name'
    FROM (
        (SELECT ROUTINE_NAME SPName
            FROM INFORMATION_SCHEMA.ROUTINES 
            WHERE ROUTINE_DEFINITION LIKE '%' + @SearchText + '%' 
            AND ROUTINE_TYPE='PROCEDURE')
        UNION ALL
        (SELECT OBJECT_NAME(id) SPName
            FROM SYSCOMMENTS 
            WHERE [text] LIKE '%' + @SearchText + '%' 
            AND OBJECTPROPERTY(id, 'IsProcedure') = 1 
            GROUP BY OBJECT_NAME(id))
        UNION ALL
        (SELECT OBJECT_NAME(object_id) SPName
            FROM sys.sql_modules
            WHERE OBJECTPROPERTY(object_id, 'IsProcedure') = 1
            AND definition LIKE '%' + @SearchText + '%')
    ) AS sp
    CROSS APPLY (select SPECIFIC_SCHEMA from INFORMATION_SCHEMA.ROUTINES r where r.ROUTINE_NAME = sp.SPName) sc
    ORDER BY 1
    
    0 讨论(0)
  • 2020-11-28 17:52
    SELECT name FROM sys.procedures WHERE Object_definition(object_id) LIKE '%FOO%'
    
    0 讨论(0)
  • 2020-11-28 17:52
    sp_msforeachdb 'use ?;select name,''?'' from sys.procedures where object_definition(object_id) like ''%text%'''
    

    This will search in all stored procedure of all databases. This will also work for long procedures.

    0 讨论(0)
提交回复
热议问题