如何找到包含<text>的存储过程?


Answers:


557
SELECT ROUTINE_NAME, ROUTINE_DEFINITION
    FROM INFORMATION_SCHEMA.ROUTINES 
    WHERE ROUTINE_DEFINITION LIKE '%Foo%' 
    AND ROUTINE_TYPE='PROCEDURE'

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

SELECT OBJECT_NAME(object_id)
    FROM sys.sql_modules
    WHERE OBJECTPROPERTY(object_id, 'IsProcedure') = 1
    AND definition LIKE '%Foo%'

61
请注意,以防万一您需要很长的过程,routine_definition会被裁剪为4000个字符。请改用OBJECTPROPERTY方法。
PeteT

1
根据执行计划,第三个变体似乎是获取此数据的最有效方式。
TMcManemy

1
@PeteT说的一百万倍。几年前,这个4000个字符的限制确实使我难过。
维维安河

14
您能否对这3个查询分别做些什么以及它们之间的区别有什么解释?
Tot Zam's

3
最好提供一些细节,以了解每个查询的工作方式以及它们之间的不同之处,或者为什么一个比另一个更好。这些答案会鼓励经验不足的工程师盲目/粘贴,而对于使用新的RDBMS(例如SQL Server)的高级工程师则感到沮丧。
DavidScherer '18 -10-19

78

索取免费的 Red-Gate SQL Search工具的副本,然后开始享受在SQL Server中进行搜索的乐趣!:-)

在此处输入图片说明

这是一个非常有用的工具,是的!对于任何使用,它都是完全免费的。


5
这是一个很棒的工具,在我提出这个问题之前,我已经使用过它。值得注意的是,默认情况下它将搜索结果限制为150个。查看此论坛帖子,以获取有关如何增加该限制的信息。
斯科特

1
它是超快速且可用的工具。
Suat Atan PhD

功能有限,但工具很棒。
艾西瓦娅·希瓦


32

我接受了Kashif的回答,并将他们全部合并在一起。奇怪的是,有时我在选择项之一中找到了结果,但没有找到其他结果。为了安全起见,我在寻找某物时会同时运行这3个程序。希望这可以帮助:

DECLARE @SearchText varchar(1000) = 'mytext';

SELECT DISTINCT SPName 
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 T
ORDER BY T.SPName

12

如果还需要模式:

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 

10
SELECT ROUTINE_NAME, ROUTINE_DEFINITION 
FROM INFORMATION_SCHEMA.ROUTINES 
WHERE ROUTINE_DEFINITION LIKE '%FieldName%' 
AND ROUTINE_TYPE='PROCEDURE'

2
但是要小心,因为它只是一个子字符串匹配项。“ FieldName”可能出现在注释,字符串常量或类似的内容中。另外,我怀疑寻找是否[whatever]会流行whatever(当您寻找符合模式的标识符时,它会变得更加重要)。
CVn

2
请参阅上面接受的答案中的@PeteT的评论。INFORMATION_SCHEMA.ROUTINES.ROUTINE_DEFINITION被裁切为4000个字符。因此,如果存储过程的长度超过4000个字符,则此方法将不起作用。
TMcManemy 2013年

7

我尝试了上面的示例,但它显示的字符数不超过4000个,因此我对其进行了一点点修改,并能够获得整个存储过程的定义。请在下面查看更新的脚本-

SELECT SCHEMA_NAME(O.SCHEMA_ID) [SCHEMA_NAME], O.NAME, OBJECT_DEFINITION(OBJECT_ID) 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  OBJECT_DEFINITION(OBJECT_ID) LIKE '%FOO%'
ORDER BY  SCHEMA_NAME(O.SCHEMA_ID), O.NAME

6

首先,请确保您在用户凭据下以及正确的数据库上下文中运行查询。

USE YOUR_DATABASE_NAME;

否则,sys.procedures将不会返回任何内容。现在运行查询,如下所示:

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

另一个选择是使用INFORMATION_SCHEMA.ROUTINES.ROUTINE_DEFINITION,但是要注意,它仅包含例程中有限数量的字符(即前4000个字符)。

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

我在Microsoft SQL Server 2008 R2(SP1)-10.50.2500.0(X64)上进行了测试



2

这是使用Losbear制作但经过修改的联合版本,因此它也输出模式名称:

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


2

对于比SQL Server 2000更高的任何SQL Server:

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

如果有人卡在SQL Server 2000上,则表sql_modules不存在,因此您将使用syscomments,您将获得多个存储过程的记录,这些记录大于4000个字符,但是它们具有相同的c.number字段,因此可以进行分组将各部分放在一起以获得完整的存储过程文本:

    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        


1
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
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

该查询返回在存储过程中引用了“ XXXX”的任何存储过程的名称和内容。

这在查找引用特定表/视图/过程的过程时非常有用


0
SELECT * FROM sys.procedures WHERE Name LIKE '%CompanySpecialisation%'

这就是我在SQL 2012中编写的,用于选择名称如“ CompanySpecialisation”的所有存储过程


0

在SP中查找文本的存储过程。{Dinesh Baskaran} Trendy Global Systems pvt ltd

  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'  
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
SELECT OBJECT_NAME(id)     
FROM syscomments 
WHERE [text] LIKE '%Name%' 
AND OBJECTPROPERTY(id, 'IsProcedure') = 1 
GROUP BY OBJECT_NAME(id)

尝试这个 .....


0
sp_msforeachdb 'use ?;select name,''?'' from sys.procedures where object_definition(object_id) like ''%text%'''

这将搜索所有数据库的所有存储过程。这也适用于较长的过程。


0

我使用这个脚本。如果您将XML注释更改为在黄色背景上显示为黑色文本,则可以在结果的xml列中突出显示要查找的文本。(工具->选项->环境->字体和颜色[显示项目:XML注释]

    ---------------------------------------------
    --------------   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     -----------------
    ---------------------------------------------
    ---------------------------------------------
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.