Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all the stored procedures from all databases in SQL Server 2008

Can anyone please advise on a way to retrieve a listing of all stored procedures along with their schema names in all database? Thanks for your input!

like image 345
Hugo Avatar asked Mar 30 '13 23:03

Hugo


1 Answers

CREATE TABLE #x(db SYSNAME, s SYSNAME, p SYSNAME);

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql += N'INSERT #x SELECT ''' + name + ''',s.name, p.name
  FROM ' + QUOTENAME(name) + '.sys.schemas AS s
  INNER JOIN ' + QUOTENAME(name) + '.sys.procedures AS p
  ON p.schema_id = s.schema_id;
' FROM sys.databases WHERE database_id > 4

EXEC sp_executesql @sql;

SELECT db,s,p FROM #x ORDER BY db,s,p;

DROP TABLE #x;
like image 182
Aaron Bertrand Avatar answered Jan 04 '23 00:01

Aaron Bertrand