在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\]%'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值