在SQL Server中的存储过程中search文本

我想从我的所有数据库存储过程中search文本。 我使用下面的SQL:

SELECT DISTINCT o.name AS Object_Name, o.type_desc FROM sys.sql_modules m INNER JOIN sys.objects o ON m.object_id = o.object_id WHERE m.definition Like '%[ABD]%'; 

我想在包括方括号的所有存储过程中search[ABD] ,但是没有给出正确的结果。 我怎样才能改变我的查询来实现这一目标?

退出方括号:

 ... WHERE m.definition Like '%\[ABD\]%' ESCAPE '\' 

然后方括号将被视为string文字而不是通配符。

试试这个请求:

询问

 SELECT name FROM sys.procedures WHERE Object_definition(object_id) LIKE '%strHell%' 

你有没有尝试过使用一些第三方工具来做search? 在那里有几个免费的,过去为我节省了大量的时间。

下面是我使用的两个SSMS Addins,取得了很好的成功。

ApexSQLsearch – search数据库中的架构和数据,并具有附加function,如依赖关系跟踪和更多…

SSMS工具包 – 具有与上一个相同的searchfunction和其他一些很酷的function。 不是免费的SQL Server 2012,但仍然非常实惠。

我知道这个答案不是100%与问题有关(更具体),但希望别人会发现这个问题很有用。

我通常运行以下来实现:

 select distinct object_name(id) from syscomments where text like '%[ABD]%' order by object_name(id) 

Redgate的SQL Search是一个很好的工具,它是SSMS的免费插件。

好的做法与SQL Server一起工作。

在下面的存储过程中,为底部图像设置快捷键,

 CREATE PROCEDURE [dbo].[Searchinall] (@strFind AS VARCHAR(MAX)) AS BEGIN SET NOCOUNT ON; --TO FIND STRING IN ALL PROCEDURES BEGIN SELECT OBJECT_NAME(OBJECT_ID) SP_Name ,OBJECT_DEFINITION(OBJECT_ID) SP_Definition FROM sys.procedures WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%'+@strFind+'%' END --TO FIND STRING IN ALL VIEWS BEGIN SELECT OBJECT_NAME(OBJECT_ID) View_Name ,OBJECT_DEFINITION(OBJECT_ID) View_Definition FROM sys.views WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%'+@strFind+'%' END --TO FIND STRING IN ALL FUNCTION BEGIN SELECT ROUTINE_NAME Function_Name ,ROUTINE_DEFINITION Function_definition FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_DEFINITION LIKE '%'+@strFind+'%' AND ROUTINE_TYPE = 'FUNCTION' ORDER BY ROUTINE_NAME END --TO FIND STRING IN ALL TABLES OF DATABASE. BEGIN SELECT t.name AS Table_Name ,c.name AS COLUMN_NAME FROM sys.tables AS t INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID WHERE c.name LIKE '%'+@strFind+'%' ORDER BY Table_Name END END 

现在 – 设置如下短键,

在这里输入图像说明

所以下次只要你想在Store procedureViewsFunctionsTables这四个对象中find一个特定的文本。 您只需要编写该关键字并按下快捷键。

例如:我想search'PaymentTable',然后在查询编辑器中input'PaymentTable',然后按下快捷键ctrl+4 – 它会为您提供完整的结果。

你也可以使用这个:

 SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_DEFINITION like '%Search_String%' 
 select top 10 * from sys.procedures where object_definition(object_id) like '%\[ABD\]%' 

它可能会帮助你!

 SELECT DISTINCT A.NAME AS OBJECT_NAME, A.TYPE_DESC FROM SYS.SQL_MODULES M INNER JOIN SYS.OBJECTS A ON M.OBJECT_ID = A.OBJECT_ID WHERE M.DEFINITION LIKE '%['+@SEARCH_TEXT+']%' ORDER BY TYPE_DESC 
 SELECT DISTINCT o.name AS Object_Name, o.type_desc FROM sys.sql_modules m INNER JOIN sys.objects o ON m.object_id = o.object_id WHERE m.definition Like '%[String]%'; 

请把它当作一个“肮脏”的select,但是这使我保存了很多次,特别是当我不熟悉DB项目的时候。 有时你试图在所有的SP中search一个string,并忘记了一些相关的逻辑可能已经隐藏在函数和触发器之间,或者它可以用不同于你想象的方式。

从您的MSSMS您可以右键单击您的数据库并selectTasks -> Generate Scripts向导输出所有的SP,Fns和触发器到一个单一的.sql文件。

在这里输入图像说明

请务必select触发器!

在这里输入图像说明

然后只需使用Sublime或记事本search您需要查找的string。 我知道这可能是相当低效和偏执的方法,但它的工作:)

使用CHARINDEX :

 SELECT DISTINCT o.name AS Object_Name,o.type_desc FROM sys.sql_modules m INNER JOIN sys.objects o ON m.object_id=o.object_id WHERE CHARINDEX('[ABD]',m.definition) >0 ; 

使用PATINDEX :

 SELECT DISTINCT o.name AS Object_Name,o.type_desc FROM sys.sql_modules m INNER JOIN sys.objects o ON m.object_id=o.object_id WHERE PATINDEX('[[]ABD]',m.definition) >0 ; 

使用这个double [[]ABD]类似于转义:

 WHERE m.definition LIKE '%[[]ABD]%' 

你也可以使用:

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

这包括评论

 /* SEARCH SPROCS & VIEWS The following query will allow search within the definitions of stored procedures and views. It spits out the results as XML, with the full definitions, so you can browse them without having to script them individually. */ /* STEP 1: POPULATE SEARCH KEYS. (Set to NULL to ignore) */ DECLARE @def_key varchar(128) = '%foo%', /* <<< definition search key */ @name_key varchar(128) = '%bar%', /* <<< name search key */ @schema_key varchar(128) = 'dbo'; /* <<< schema search key */ ;WITH SearchResults AS ( /* STEP 2: DEFINE SEARCH QUERY AS CTE (Common Table Expression) */ SELECT [Object].object_id AS [object_id], [Schema].name AS [schema_name], [Object].name AS [object_name], [Object].type AS [object_type], [Object].type_desc AS [object_type_desc], [Details].definition AS [module_definition] FROM /* sys.sql_modules = where the body of sprocs and views live */ sys.sql_modules AS [Details] WITH (NOLOCK) JOIN /* sys.objects = where the metadata for every object in the database lives */ sys.objects AS [Object] WITH (NOLOCK) ON [Details].object_id = [Object].object_id JOIN /* sys.schemas = where the schemas in the datatabase live */ sys.schemas AS [Schema] WITH (NOLOCK) ON [Object].schema_id = [Schema].schema_id WHERE (@def_key IS NULL OR [Details].definition LIKE @def_key) /* <<< searches definition */ AND (@name_key IS NULL OR [Object].name LIKE @name_key) /* <<< searches name */ AND (@schema_key IS NULL OR [Schema].name LIKE @schema_key) /* <<< searches schema */ ) /* STEP 3: SELECT FROM CTE INTO XML */ /* This outer select wraps the inner queries in to the <sql_object> root element */ SELECT ( /* This inner query maps stored procedure rows to <procedure> elements */ SELECT TOP 100 PERCENT [object_id] AS [@object_id], [schema_name] + '.' + [object_name] AS [@full_name], [module_definition] AS [module_definition] FROM SearchResults WHERE object_type = 'P' ORDER BY [schema_name], [object_name] FOR XML PATH ('procedure'), TYPE ) AS [procedures], /* <<< as part of the outer query, this alias causes the <procedure> elements to be wrapped within the <procedures> element */ ( /* This inner query maps view rows to <view> elements */ SELECT TOP 100 PERCENT [object_id] AS [@object_id], [schema_name] + '.' + [object_name] AS [@full_name], [module_definition] AS [module_definition] FROM SearchResults WHERE object_type = 'V' ORDER BY [schema_name], [object_name] FOR XML PATH ('view'), TYPE ) AS [views] /* <<< as part of the outer query, this alias causes the <view> elements to be wrapped within the <views> element */ FOR XML PATH ('sql_objects') 
 Select distinct OBJECT_NAME(id) from syscomments where text like '%string%' AND OBJECTPROPERTY(id, 'IsProcedure') = 1 

每隔一段时间,我都会使用这个脚本来确定哪些过程需要修改,或者找出使用哪一列表或者哪个表来删除一些旧垃圾。 它通过奇妙地提供的sp_msforeachdb检查它运行的实例上的每个数据库。

 if object_id('tempdb..##nothing') is not null drop table ##nothing CREATE TABLE ##nothing ( DatabaseName varchar(30), SchemaName varchar(30), ObjectName varchar(100), ObjectType varchar(50) ) EXEC master.sys.sp_msforeachdb 'USE ? insert into ##nothing SELECT db_name() AS [Database], [Scehma]=schema_name(o.schema_id), o.Name, o.type FROM sys.sql_modules m INNER JOIN sys.objects o ON o.object_id = m.object_id WHERE m.definition like ''%SOME_TEXT%''' --edit this text SELECT * FROM ##nothing n order by OBJECTname 
  SELECT DISTINCT OBJECT_NAME([id])--,syscomments.* this gives name of --stored procedures that text found FROM syscomments WHERE [id] IN (SELECT [id] FROM sysobjects WHERE xtype IN ('TF','FN','V','P') AND status >= 0) AND ([text] LIKE '%text to be search%' ) 

OBJECT_NAME([id]) – >对象名称(查看,存储过程,标量函数,表函数名称)

id(int)=对象标识号

xtype char(2)对象types。 可以是以下对象types之一:

FN =标量函数

P =存储过程

V =查看

TF =表函数

 -- Applicable for SQL 2005+ USE YOUR_DATABASE_NAME //; GO SELECT [Scehma] = schema_name(o.schema_id) ,o.NAME ,o.type FROM sys.sql_modules m INNER JOIN sys.objects o ON o.object_id = m.object_id WHERE m.DEFINITION LIKE '%YOUR SEARCH KEYWORDS%' GO 

我创build了一个search过程,表,视图或作业的过程。 第一个参数@search是search标准,@target仅用于指定过程或其他。 如果没有指定,它将search所有。 @db是指定要search的数据库。 这是查询。

 ALTER PROCEDURE [dbo].[usp_find_objects] ( @search VARCHAR(128), @target VARCHAR(128) = NULL, @db VARCHAR(20) = NULL ) AS SET NOCOUNT ON; DECLARE @TSQL NVARCHAR(MAX), @USEDB NVARCHAR(30) IF @db IS NOT NULL SET @USEDB = 'USE ' + @db ELSE SET @USEDB = '' IF @target IS NULL SET @target = '' SET @TSQL = @USEDB + ' DECLARE @search VARCHAR(128) DECLARE @target VARCHAR(128) SET @search = ''%' + @search + '%'' SET @target = ''' + @target + ''' IF @target LIKE ''%Procedure%'' BEGIN SELECT o.name As ''Stored Procedures'' FROM SYSOBJECTS o INNER JOIN SYSCOMMENTS c ON o.id = c.id WHERE c.text LIKE @search AND o.xtype = ''P'' GROUP BY o.name ORDER BY o.name END ELSE IF @target LIKE ''%View%'' BEGIN SELECT o.name As ''Views'' FROM SYSOBJECTS o INNER JOIN SYSCOMMENTS c ON o.id = c.id WHERE c.text LIKE @search AND o.xtype = ''V'' GROUP BY o.name ORDER BY o.name END ELSE IF @target LIKE ''%Table%'' BEGIN SELECT o.name As ''Tables'' FROM SYSOBJECTS o INNER JOIN SYSCOMMENTS c ON o.id = c.id WHERE c.text LIKE @search AND o.xtype = ''U'' GROUP BY o.name ORDER BY o.name END ELSE IF @target LIKE ''%Job%'' BEGIN SELECT j.job_id, s.srvname, j.name, js.step_id, js.command, j.enabled FROM [msdb].dbo.sysjobs j JOIN [msdb].dbo.sysjobsteps js ON js.job_id = j.job_id JOIN master.dbo.sysservers s ON s.srvid = j.originating_server_id WHERE js.command LIKE @search END ELSE BEGIN SELECT o.name As ''Stored Procedures'' FROM SYSOBJECTS o INNER JOIN SYSCOMMENTS c ON o.id = c.id WHERE c.text LIKE @search AND o.xtype = ''P'' GROUP BY o.name ORDER BY o.name SELECT o.name As ''Views'' FROM SYSOBJECTS o INNER JOIN SYSCOMMENTS c ON o.id = c.id WHERE c.text LIKE @search AND o.xtype = ''V'' GROUP BY o.name ORDER BY o.name SELECT o.name As ''Tables'' FROM SYSCOLUMNS c INNER JOIN SYSOBJECTS o ON c.id = c.id INNER JOIN MASTER.DBO.SYSTYPES t ON c.xtype = t.xtype WHERE o.name LIKE @search AND o.Type = ''U'' GROUP BY o.name ORDER BY o.name SELECT j.name AS ''Jobs'' FROM [msdb].dbo.sysjobs j JOIN [msdb].dbo.sysjobsteps js ON js.job_id = j.job_id JOIN master.dbo.sysservers s ON s.srvid = j.originating_server_id WHERE js.command LIKE @search END ' EXECUTE sp_executesql @TSQL 

该查询是来自所有数据库的存储过程中的search文本。

 DECLARE @T_Find_Text VARCHAR(1000) = 'Foo' IF OBJECT_ID('tempdb..#T_DBNAME') IS NOT NULL DROP TABLE #T_DBNAME IF OBJECT_ID('tempdb..#T_PROCEDURE') IS NOT NULL DROP TABLE #T_PROCEDURE CREATE TABLE #T_DBNAME ( IDX int IDENTITY(1,1) PRIMARY KEY , DBName VARCHAR(255) ) CREATE TABLE #T_PROCEDURE ( IDX int IDENTITY(1,1) PRIMARY KEY , DBName VARCHAR(255) , Procedure_Name VARCHAR(MAX) , Procedure_Description VARCHAR(MAX) ) INSERT INTO #T_DBNAME (DBName) SELECT name FROM master.dbo.sysdatabases DECLARE @T_C_IDX INT = 0 DECLARE @T_C_DBName VARCHAR(255) DECLARE @T_SQL NVARCHAR(MAX) DECLARE @T_SQL_PARAM NVARCHAR(MAX) SET @T_SQL_PARAM = ' @T_C_DBName VARCHAR(255) , @T_Find_Text VARCHAR(255) ' WHILE EXISTS(SELECT TOP 1 IDX FROM #T_DBNAME WHERE IDX > @T_C_IDX ORDER BY IDX ASC) BEGIN SELECT TOP 1 @T_C_DBName = DBName FROM #T_DBNAME WHERE IDX > @T_C_IDX ORDER BY IDX ASC SET @T_SQL = '' SET @T_SQL = @T_SQL + 'INSERT INTO #T_PROCEDURE(DBName, Procedure_Name, Procedure_Description)' SET @T_SQL = @T_SQL + 'SELECT SPECIFIC_CATALOG, ROUTINE_NAME, ROUTINE_DEFINITION ' SET @T_SQL = @T_SQL + 'FROM ' + @T_C_DBName + '.INFORMATION_SCHEMA.ROUTINES ' SET @T_SQL = @T_SQL + 'WHERE ROUTINE_DEFINITION LIKE ''%''+ @T_Find_Text + ''%'' ' SET @T_SQL = @T_SQL + 'AND ROUTINE_TYPE = ''PROCEDURE'' ' BEGIN TRY EXEC SP_EXECUTESQL @T_SQL, @T_SQL_PARAM, @T_C_DBName, @T_Find_Text END TRY BEGIN CATCH SELECT @T_C_DBName + ' ERROR' END CATCH SET @T_C_IDX = @T_C_IDX + 1 END SELECT IDX, DBName, Procedure_Name FROM #T_PROCEDURE ORDER BY DBName ASC 

也试试这个:

  SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_DEFINITION like '%\[ABD\]%' 

你也可以使用

 CREATE PROCEDURE [Search]( @Filter nvarchar(max) ) AS BEGIN SELECT name FROM procedures WHERE definition LIKE '%'+@Filter+'%' END 

然后运行

 exec [Search] 'text'