Query to list all stored procedures

Viewed 728815

What query can return the names of all the stored procedures in a SQL Server database

If the query could exclude system stored procedures, that would be even more helpful.

24 Answers

As Mike stated, the best way is to use information_schema. As long as you're not in the master database, system stored procedures won't be returned.

SELECT * 
  FROM DatabaseName.INFORMATION_SCHEMA.ROUTINES
 WHERE ROUTINE_TYPE = 'PROCEDURE'

If for some reason you had non-system stored procedures in the master database, you could use the query (this will filter out MOST system stored procedures):

SELECT * 
  FROM [master].INFORMATION_SCHEMA.ROUTINES
 WHERE ROUTINE_TYPE = 'PROCEDURE' 
   AND LEFT(ROUTINE_NAME, 3) NOT IN ('sp_', 'xp_', 'ms_')
SELECT name, 
       type
  FROM dbo.sysobjects
 WHERE (type = 'P')

From my understanding the "preferred" method is to use the information_schema tables:

select * 
  from information_schema.routines 
 where routine_type = 'PROCEDURE'

If you are using SQL Server 2005 the following will work:

select *
  from sys.procedures
 where is_ms_shipped = 0

Just the names:

SELECT SPECIFIC_NAME  
FROM YOUR_DB_NAME.information_schema.routines  
WHERE routine_type = 'PROCEDURE'

Unfortunately INFORMATION_SCHEMA doesn't contain info about the system procs.

SELECT *
  FROM sys.objects
 WHERE objectproperty(object_id, N'IsMSShipped') = 0
   AND objectproperty(object_id, N'IsProcedure') = 1

the best way to get objects is use sys.sql_modules. you can find every thing that you want from this table and join this table with other table to get more information by object_id

SELECT o. object_id,o.name AS name,o.type_desc,m.definition,schemas.name scheamaName
FROM sys.sql_modules        m 
    INNER JOIN sys.objects  o ON m.object_id=o.OBJECT_ID
    INNER JOIN sys.schemas ON schemas.schema_id = o.schema_id
    WHERE [TYPE]='p'
select *  
  from dbo.sysobjects
 where xtype = 'P'
   and status > 0

This will give just the names of the stored procedures.

select specific_name
from information_schema.routines
where routine_type = 'PROCEDURE';

This is gonna show all the stored procedures and the code:

select sch.name As [Schema], obj.name AS [Stored Procedure], code.definition AS [Code] from sys.objects as obj
    join sys.sql_modules as code on code.object_id = obj.object_id
    join sys.schemas as sch on sch.schema_id = obj.schema_id
    where obj.type = 'P'
USE DBNAME

select ROUTINE_NAME from information_schema.routines 
where routine_type = 'PROCEDURE'


GO 

This will work on mssql.

Related