如何在不知道数据库名称的情况下查找存储过

时间:2017-07-12 09:36:53

标签: sql-server stored-procedures

谁能告诉我如何找到存储过程的代码? 我甚至不知道数据库名称,我怎么能找到它。 请帮帮我这个

2 个答案:

答案 0 :(得分:0)

您可以使用以下代码在所有数据库中搜索SP。

DECLARE @SPROCS AS TABLE (DBNAME VARCHAR(64), SCHEMANAME VARCHAR (128), 
STOREDPROCNAME VARCHAR(128), 
TYPE VARCHAR(10), TYPE_DESC VARCHAR(128), 
CREATE_DATE DATETIME, MODIFY_DATE DATETIME, IS_MS_SHIPPED INT)
INSERT INTO @SPROCS
EXECUTE SP_MSFOREACHDB N'USE ?; SELECT ''?'', 
S.NAME AS SCHEMANAME, O.NAME AS STOREDPROCNAME, 
TYPE, TYPE_DESC, CREATE_DATE, MODIFY_DATE, IS_MS_SHIPPED
FROM SYS.ALL_OBJECTS AS O INNER JOIN SYS.SCHEMAS AS S 
ON S.SCHEMA_ID = O.SCHEMA_ID 
WHERE O.TYPE IN (''P'',''V'',''PC'',''FN'',''TF'',''U'')
AND IS_MS_SHIPPED = 0'

SELECT * FROM @SPROCS
WHERE DBNAME NOT IN ('MASTER', 'TEMPDB', 'MODEL', 'MSDB', 'DBA') AND STOREDPROCNAME LIKE '%<SP Name>%'
ORDER BY DBNAME, SCHEMANAME, TYPE, STOREDPROCNAME

选项2

EXEC sp_msforeachdb 
'if exists(select 1 from [?].sys.objects where name=''<SPname>'')
select ''?'' as FoundInDb from [?].sys.objects where name=''<SPname>'''

选项3

如果可能,您可以安装RedGate SQLSearch的工具轻松完成

答案 1 :(得分:0)

SELECT * 来自dbo.sysobjects WHERE type ='P'