SQL Server Management Studio - find stored procedure by name over multiple databases

China☆狼群 提交于 2019-12-02 11:48:26
Alfaiz Ahmed

You can use dynamic SQL to check procedure over all of databases in SQL Server Management Studio

USE MASTER
GO
BEGIN TRAN

DECLARE @strt INT,@End INT,@Database NVARCHAR(50)

SELECT * INTO #T FROM Sys.databases WITH(NOLOCK) WHERE database_id>4 
ORDER BY 1

SELECT ROW_NUMBER ()OVER (ORDER BY database_Id)Db_Id,* INTO #TT FROM #T
SET @strt=1
SELECT @End=Max(Db_ID)FROM #tt

WHILE @strt<=@END
     BEGIN
         DECLARE @string NVARCHAR(MAX)
         SELECT @Database=NAME FROM #TT WHERE Db_ID=@strt

        Set @string='  Select '''+@Database+'''db_Name,* from '+@Database+'.sys.objects 
          WHERE Name=''XYZ_procedure'''

          SET @strt=@strt+1
          PRINT @string
          EXEC(@string)
     END

ROLLBACK TRAN

If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).

It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??

If I am getting it right. You need a way to filter the procedures by name in SSMS. You can refer the following link :

Find Using Filter Settings In Object Explorer

Denis Rubashkin

I think the simplest way is to use the undocumented stored procedure sp_MSForeachdb which executes a command for each database:

EXEC sp_MSforeachdb 
'
USE ?
IF EXISTS (
    SELECT 1 
    FROM sys.objects
    WHERE name = ''XYZ_procedure''
    )
    SELECT DB_NAME();
'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!