我需要搜索一个SQL server 2008的存储过程包含哪里可能 数据库字段或变量名的名称。
当前回答
SELECT name FROM sys.procedures WHERE Object_definition(object_id) LIKE '%FOO%'
其他回答
SELECT ROUTINE_NAME, ROUTINE_DEFINITION
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%FieldName%'
AND ROUTINE_TYPE='PROCEDURE'
首先确保在用户凭证下运行查询,并且在正确的数据库上下文中运行查询。
USE YOUR_DATABASE_NAME;
否则,系统。过程不会返回任何东西。现在运行查询如下所示:
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)上进行测试
试试下面的代码:
DECLARE @SearchText NVARCHAR(2000) = N'YourText';
SELECT DISTINCT SCHEMA_NAME(o.schema_id),o.name,[text] AS MainTextProc
FROM sys.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 c.[text] LIKE '%'+ @SearchText +'%'
ORDER BY SCHEMA_NAME(o.schema_id),o.[name]
select * from sys.system_objects
where name like '%cdc%'
我使用这个脚本。如果您将XML Comments更改为显示为黄色背景上的黑色文本,则可以在结果的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 -----------------
---------------------------------------------
---------------------------------------------
推荐文章
- 在SQL Server中查找重复的行
- 执行带有Output参数的存储过程?
- 从DateTime中提取小时(SQL Server 2005)
- 我如何在T-SQL用逗号格式化一个数字?
- LEFT OUTER JOIN如何返回比左表中存在的记录更多的记录?
- 如何用SQL语句计算百分比
- SQL Server动态PIVOT查询?
- 如何等待2秒?
- SQL Server: CROSS JOIN和FULL OUTER JOIN的区别是什么?
- varchar和nvarchar SQL Server数据类型之间的主要性能差异是什么?
- 向现有表添加主键
- 如何在SQL Server中恢复到不同的数据库?
- 我应该在SQL varchar(长度)中考虑电话的最长的全球电话号码是什么
- T-SQL CASE子句:如何指定WHEN NULL
- 表中标识列的显式值只能在使用列列表且IDENTITY_INSERT为ON SQL Server时指定