How do I find a stored procedure containing ?

后端 未结 20 2025
梦谈多话
梦谈多话 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:38
    create Procedure [dbo].[TextFinder]
    (@Text varchar(500),@Type varchar(2)=NULL) 
    AS
    BEGIN
    
    
    
    SELECT DISTINCT o.name AS ObjectName,
    CASE o.xtype
    
    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'`enter code here`
    
    WHEN 'U' THEN 'User table'
    
    WHEN 'V' THEN 'View'
    
    WHEN 'X' THEN 'Extended stored procedure'
    
    ELSE o.xtype
    
    END AS ObjectType,
    
    ISNULL( p.Name, '[db]') AS Location
    
    FROM syscomments c
    
    INNER JOIN sysobjects o ON c.id=o.id
    
    LEFT JOIN sysobjects p ON o.Parent_obj=p.id
    
    WHERE c.text LIKE '%' + @Text + '%' and
    
    o.xtype = case when @Type IS NULL then o.xtype  else @Type end
    
    ORDER BY Location, ObjectName
    
    END
    
    0 讨论(0)
  • 2020-11-28 17:40

    First ensure that you're running the query under your user credentials, and also in the right database context.

    USE YOUR_DATABASE_NAME;
    

    Otherwise, sys.procedures won't return anything. Now run the query as below:

    select * from sys.procedures p 
    join sys.syscomments s on p.object_id = s.id 
    where text like '%YOUR_TEXT%';
    

    Another option is to use INFORMATION_SCHEMA.ROUTINES.ROUTINE_DEFINITION, but be aware that it only holds limited number of characters (i.e., first 4000 characters) of the routine.

    select * from YOUR_DATABASE_NAME.INFORMATION_SCHEMA.ROUTINES
    where ROUTINE_DEFINITION like '%YOUR_TEXT%';
    

    I tested on Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64)

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

    You can also try ApexSQL Search - free SSMS plug-in from ApexSQL.

    enter image description here

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

    In case you needed schema as well:

    SELECT   DISTINCT SCHEMA_NAME(o.schema_id),o.name,[text]
    FROM     syscomments AS c
             INNER JOIN sys.objects AS o ON c.id = o.[object_id]
             INNER JOIN sys.schemas AS s ON o.schema_id = s.schema_id
    WHERE    text LIKE '%foo%'
    ORDER BY  SCHEMA_NAME(o.schema_id),o.name 
    
    0 讨论(0)
  • 2020-11-28 17:42
    SELECT ROUTINE_NAME, ROUTINE_DEFINITION 
    FROM INFORMATION_SCHEMA.ROUTINES 
    WHERE ROUTINE_DEFINITION LIKE '%FieldName%' 
    AND ROUTINE_TYPE='PROCEDURE'
    
    0 讨论(0)
  • 2020-11-28 17:42

    How to Find a Stored Procedure Containing Text or String

    Many time we need to find the text or string in the stored procedure. Here is the query to find the containing text.

    SELECT OBJECT_NAME(id) 
    FROM SYSCOMMENTS 
    WHERE [text] LIKE '%Text%' 
    AND OBJECTPROPERTY(id, 'IsProcedure') = 1 
    GROUP BY OBJECT_NAME(id)
    

    For more information please check the given URL given below.

    http://www.freshcodehub.com/Article/34/how-to-find-a-stored-procedure-containing-text-or-string

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