Skip to content
Advertisement

Check if procedure exists in any database SQL Server

I’d like to know if there is any way to search for a procedure in all the databases of a server (there are only SQL Server databases on that server).

So far I’ve only found how to find a stored procedure in a certain database with Object_id(), but it would take too long for me to manually search each database by hand. I’d also like to see in which database is the stored procedure applied.

Code I’ve found so far:

select * 
from MyDataBase.sys.objects 
where object_id = object_id(N'MyProcedure')

Advertisement

Answer

You can try this code

CREATE TABLE #SPs (db_name varchar(100), name varchar(100), object_id int)

EXEC sp_msforeachdb ‘USE [?]; INSERT INTO #SPs select ”?”, name, object_id from sys.procedures’

SELECT * FROM #SPs

See also :https://dba.stackexchange.com/questions/130399/script-to-find-the-list-of-stored-procedures-in-all-databases

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement