我在我的数据库中创建了许多存储过程。 但我需要在其定义中搜索包含“welcome to”的存储过程。 我可以通过sp_helptext检查每个存储过程 我有很多存储过程因此我无法使用sp_help检查每个存储过程,因为它耗时
请帮助,如果可以
答案 0 :(得分:2)
使用 INFORMATION_SCHEMA.ROUTINES
。
SELECT *
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%welcome to%'
AND ROUTINE_TYPE = 'PROCEDURE'
或使用 sys.sql_modules
SELECT sm.object_id,
Object_name(sm.object_id) AS object_name,
sm.definition
FROM sys.sql_modules AS sm
JOIN sys.objects AS o
ON sm.object_id = o.object_id
WHERE type_desc = 'SQL_STORED_PROCEDURE'
AND definition LIKE'%welcome to%'
答案 1 :(得分:1)
您可以使用系统视图SYS.SQL_MODULES
和SYS.OBJECTS
我创建了存储过程来查找任何类型的对象中的任何文本
检查下面
CREATE PROCEDURE [SearchFromObjects]
(
@SearchString NVARCHAR(500) = NULL,
@SearchObjectList VARCHAR(50) = NULL
)
AS
BEGIN
SET NOCOUNT ON
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
DECLARE @sqlQuery NVARCHAR(1000)
SELECT @SearchObjectList = ''''+REPLACE(@SearchObjectList, ',', ''',''')+''''
IF ISNULL(@SearchString, '') <> ''
BEGIN
SELECT @sqlQuery = ' SELECT SYS_OBJ.NAME AS [OBJECT_NAME],
CASE
WHEN SYS_OBJ.TYPE = ''P''
THEN ''PROCEDURE''
WHEN SYS_OBJ.TYPE = ''TR''
THEN ''TRIGGER''
WHEN SYS_OBJ.TYPE = ''FN''
THEN ''SCALAR FUNCTION''
WHEN SYS_OBJ.TYPE = ''TF''
THEN ''TABLE-VALUED FUNCTION''
WHEN SYS_OBJ.TYPE = ''IF''
THEN ''INLINE TABLE-VALUED FUNCTION''
WHEN SYS_OBJ.TYPE = ''V''
THEN ''VIEW''
END [OBJECT_TYPE]
FROM SYS.SQL_MODULES SYS_MOD
INNER JOIN SYS.OBJECTS SYS_OBJ ON SYS_OBJ.OBJECT_ID = SYS_MOD.OBJECT_ID
WHERE SYS_MOD.DEFINITION LIKE ''%'+@SearchString+'%''' +
CASE
WHEN ISNULL(@SearchObjectList, '') <> ''
THEN ' AND SYS_OBJ.TYPE IN('+@SearchObjectList+') '
ELSE '' END +
' ORDER BY SYS_OBJ.TYPE '
END
IF(ISNULL(@SearchString, '') = '')
BEGIN
PRINT 'Error : Incorrect Syntax...!!! '
PRINT ' '
PRINT '-----------------------------------'
PRINT 'Procedure Can take two arguments '
PRINT '-----------------------------------'
PRINT '[1] Search String : string to search in object defination (Required)'
PRINT ' Syntax : EXEC [SearchFromObjects] ''<<String to search>>'''
PRINT ' For Example : EXEC [SearchFromObjects] ''Welcome to'''
PRINT '[2] Search Object type : list of object type in which user want to search (Optional)'
PRINT ' Syntax : EXEC [SearchFromObjects] ''<<String to search>>'', ''<<List of object type seperated by comma>>'''
PRINT ' For Example : EXEC [SearchFromObjects] ''Welcome to'', ''P,TR,FN,TF,IF,V'''
PRINT ' '
PRINT 'List of allowed object types'
PRINT '---------------------------------------'
PRINT ' TYPE DESCRIPTION '
PRINT '---------------------------------------'
PRINT ' P PROCEDURE '
PRINT ' TR TRIGGER '
PRINT ' FN SCALAR FUNCTION '
PRINT ' TF TABLE-VALUED FUNCTION '
PRINT ' IF INLINE TABLE-VALUED FUNCTION '
PRINT ' V VIEW '
PRINT '---------------------------------------'
END
ELSE
BEGIN
EXECUTE(@sqlQuery)
END
SET NOCOUNT OFF
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
END
GO
只需传递两个参数1)字符串(&#39;欢迎使用&#39;)和2)对象类型(&#39; P&#39; for procedure)然后您将获得包含搜索的存储过程名称串
EXEC [SearchFromObjects] 'Welcome to', 'P'
你可以在没有任何参数的情况下执行[SearchFromObjects]来获得更多帮助