List All Stored Procedures in MS SQL Server
I needed to create a Stored Procedure in MS SQL Server.
I typically create the shell of it with:
CREATE PROC sp_MyName AS
BEGIN
SELECT '1'
END
After executing the query, I’ll close the window and go to edit it in design view. This way I can easily save my progress by running the query (as, when editing, the ALTER clause will be there to save it and not actually execute it).
But after hitting “refresh” my newly created stored procedure wasn’t there. Did I not save it? Was I in the wrong database when I originally created it?
I have several databases on this SQL Server instance, so I wanted avoid going into every single one to view the stored procedures.
My DBA was kind enough to give me this code, which will loop through all of the databases and print out the stored procedures, along with the time it was created and last modified:
I knew you could dynamically generate SQL and execute it (via EXEC) but I didn’t know that you can do this for every database in your SQL Server via sp_MSforeachdb.