在SQL Server中搜索存储过程中的文本

本文翻译自:Search text in stored procedure in SQL Server

I want to search a text from all my database stored procedures. 我想从我的所有数据库存储过程中搜索文本。 I use the below SQL: 我使用下面的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]%';

I want to search for [ABD] in all stored procedures including square brackets, but it's not giving the proper result. 我想在包括方括号在内的所有存储过程中搜索[ABD] ,但它没有给出正确的结果。 How can I change my query to achieve this? 如何更改查询以实现此目的?


#1楼

参考:https://stackoom.com/question/zhCz/在SQL-Server中搜索存储过程中的文本


#2楼

I usually run the following to achieve that: 我通常运行以下操作来实现:

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

#3楼

Escape the square brackets: 逃避方括号:

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

Then the square brackets will be treated as a string literals not as wild cards. 然后方括号将被视为字符串文字而不是通配符。


#4楼

Using CHARINDEX : 使用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 ;

Using PATINDEX : 使用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 ; 

Using this double [[]ABD] is similar to escaping : 使用这个double [[]ABD]类似于转义:

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

#5楼

Also you can use: 你也可以使用:

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

Thats include comments 这包括评论


#6楼

select top 10 * from
sys.procedures
where object_definition(object_id) like '%\[ABD\]%'
发布了0 篇原创文章 · 获赞 51 · 访问量 34万+

猜你喜欢

转载自blog.csdn.net/CHCH998/article/details/105466517
今日推荐