如何在SQL数据库中搜索所有存储过程的文本

时间:2010-06-18 21:37:40

标签: sql search ssms

在Visual Studio 2k8中,我按Ctrl-F然后键入我要在项目中搜索的一些文本。我可以一次快速搜索多个文件。

我有一个带有一些存储过程的SQL Server。如何轻松搜索存储过程中的任意字符串?我知道我可以在SQL Server Studio中列出一个存储过程的文本并从那里进行Ctrl-F搜索,但我想一次搜索它们。

6 个答案:

答案 0 :(得分:7)

SQL Search(来自Redgate)是你的朋友。免费下载,直接集成到SSMS中:)

答案 1 :(得分:4)

搜索所有视图,过程和用户​​定义的函数:

CREATE PROCEDURE usp_SearchFor
    @PatternIn AS varchar(max)
   ,@ObjectNamePattern AS varchar(max) = NULL
AS 
BEGIN
    DECLARE @Pattern AS varchar(max)
    SET @Pattern = '%' + @PatternIn + '%' ;
    WITH    ROUTINES
              AS (
                  -- CANNOT use INFORMATION_SCHEMA.ROUTINES because of 4000 character limit
        SELECT  o.type_desc AS ROUTINE_TYPE
                   ,s.[name] AS [SCHEMA_NAME]
                   ,o.[name] AS ROUTINE_NAME
                   ,m.definition AS ROUTINE_DEFINITION
            FROM    sys.sql_modules AS m WITH (NOLOCK)
            INNER JOIN sys.objects AS o WITH (NOLOCK)
                    ON m.[object_id] = o.[OBJECT_ID]
            INNER JOIN sys.schemas AS s WITH (NOLOCK)
                    ON s.[schema_id] = o.[schema_id]
            WHERE   m.definition LIKE @Pattern
                 ),
            Results
              AS (
                  SELECT    ROUTINE_TYPE
                           ,[SCHEMA_NAME]
                           ,ROUTINE_NAME
                           ,PATINDEX(@Pattern, ROUTINE_DEFINITION) AS StartPos
                           ,SUBSTRING(ROUTINE_DEFINITION,
                                      PATINDEX(@Pattern, ROUTINE_DEFINITION),
                                      255) AS WorkItem
                           ,RIGHT(ROUTINE_DEFINITION,
                                  LEN(ROUTINE_DEFINITION) - PATINDEX(@Pattern, ROUTINE_DEFINITION)) AS Remainder
                           ,1 AS Occurrence
                  FROM      ROUTINES
                  UNION ALL
                  SELECT    ROUTINE_TYPE
                           ,[SCHEMA_NAME]
                           ,ROUTINE_NAME
                           ,PATINDEX(@Pattern, Remainder) AS StartPos
                           ,SUBSTRING(Remainder, PATINDEX(@Pattern, Remainder),
                                      255) AS WorkItem
                           ,RIGHT(Remainder,
                                  LEN(Remainder) - PATINDEX(@Pattern,
                                                            Remainder)) AS Remainder
                           ,Occurrence + 1 AS Occurrence
                  FROM      Results
                  WHERE     PATINDEX(@Pattern, Remainder) > 0
                 )
        SELECT  'Search For:' + @PatternIn AS Problem
               ,ROUTINE_TYPE
               ,QUOTENAME([SCHEMA_NAME]) + '.' + QUOTENAME([ROUTINE_NAME]) AS ROUTINE_NAME
               ,WorkItem
        FROM    Results
        WHERE   @ObjectNamePattern IS NULL
                OR ROUTINE_NAME LIKE @ObjectNamePattern
        ORDER BY ROUTINE_TYPE
               ,[SCHEMA_NAME]
               ,ROUTINE_NAME
               ,Occurrence
END

答案 2 :(得分:2)

存储过程文本存储在sysobjects,syscomments中。

Example

答案 3 :(得分:1)

只需将所有过程编写到查询窗口即可。然后ctrl-f到你心中。

答案 4 :(得分:1)

SELECT DISTINCT OBJECT_NAME(ID) FROM SysComments WHERE Text LIKE '%Search%'

显然已弃用,但仍可在SQL 2008中使用

答案 5 :(得分:0)

您可以使用下面的简单搜索

select ROUTINE_name from INFORMATION_SCHEMA.ROUTINES
where CHARINDEX('demo',routine_definition,1)>0

将您的任意字符串替换为'demo'