Query to list all stored procedures
Asked Answered
M

25

419

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.

Msg answered 20/10, 2008 at 18:41 Comment(0)
I
602

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_')
Idola answered 20/10, 2008 at 19:2 Comment(4)
If you create database diagrams you might get a bunch of procs starting with 'dt_' in your database which you can also filter out.Cake
+1 for information schema. worth a read: msdn.microsoft.com/en-us/library/ms186778.aspxKetonuria
It should be "As long as you're not in the [master] or [msdb] databases,..."Trapezius
On some versions of SQL Server (such as Azure SQL Database), you have to remove the database name from the first query for it to work: SELECT * FROM INFORMATION_SCHEMA.ROUTINESMouthwash
F
130
SELECT name, 
       type
  FROM dbo.sysobjects
 WHERE (type = 'P')
Fertility answered 20/10, 2008 at 18:44 Comment(3)
This worked for me in a shared environment in MS-SQL 2008; the previous two did not...Pop
Anyone using SQL Server 2005 or newer should move away from the dbo.sys* views. This query also: filters out CLR stored procedures, does not filter out system stored procs, and returns [type] when it is known that [type] will always be 'P' since it is the WHERE condition.Trapezius
it will not work if the objects in the database has different schemaGallantry
E
37

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

select * 
  from information_schema.routines 
 where routine_type = 'PROCEDURE'
Elishaelision answered 20/10, 2008 at 18:50 Comment(1)
the returned records don't seem to have a way to differentiate the system stored proceduresWhichever
W
21

The following will Return All Procedures in selected database

SELECT * FROM sys.procedures
Wist answered 19/2, 2014 at 9:36 Comment(1)
this has got modified and create date, etc. which is very usefulHolbrooke
S
16

You can try this query to get stored procedures and functions:

SELECT name, type
FROM dbo.sysobjects
WHERE type IN (
    'P', -- stored procedures
    'FN', -- scalar functions 
    'IF', -- inline table-valued functions
    'TF' -- table-valued functions
)
ORDER BY type, name
Scutcheon answered 13/1, 2015 at 9:42 Comment(0)
K
14

You can use one of the below queries to find the list of Stored Procedures in one database :

Query1 :

    SELECT 
        *
    FROM sys.procedures;

Query2 :

    SELECT 
        * 
    FROM information_schema.routines 
    WHERE ROUTINE_TYPE = 'PROCEDURE' 

If you want to find the list of all SPs in all Databases you can use the below query :

    CREATE TABLE #ListOfSPs 
    (
        DBName varchar(100), 
        [OBJECT_ID] INT,
        SPName varchar(100)
    )

    EXEC sp_msforeachdb 'USE [?]; INSERT INTO #ListOfSPs Select ''?'', Object_Id, Name FROM sys.procedures'

    SELECT 
        * 
    FROM #ListOfSPs
Kowalczyk answered 22/2, 2017 at 16:20 Comment(1)
IMO your example using the sp_msforeachdb is gold and should be the answer. Here is a link I found talking more about this sproc: weblogs.sqlteam.com/joew/archive/2008/08/27/60700.aspxRoanne
T
10

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

select *
  from sys.procedures
 where is_ms_shipped = 0
Thies answered 20/10, 2008 at 19:27 Comment(5)
this will give the wrong result and include system like diagram stored procedure (sp_upgraddiagrams) in sql 2008Teahouse
@Piyush True that it will return the diagram procs, but someone might not consider them to be "system" procs since they didn't come with the standard install. The O.P. didn't specify how to handle that so not filtering them out is not necessarily wrong.Trapezius
@srutzky:- but still these are not sp's created by userTeahouse
@Piyush True, but as I said, the O.P. didn't specify how to handle procs that are neither "user created" nor "system". And nobody has asked.Trapezius
Great answer. Thank you for including "is_ms_shipped = 0."Dygal
A
9

Select All Stored Procedures and Views

select name,type,type_desc
from sys.objects
where type in ('V','P')
order by name,type
Auvil answered 10/7, 2017 at 20:44 Comment(0)
M
6

Just the names:

SELECT SPECIFIC_NAME  
FROM YOUR_DB_NAME.information_schema.routines  
WHERE routine_type = 'PROCEDURE'
Munniks answered 31/1, 2018 at 18:44 Comment(0)
K
5

This can also help to list procedure except the system procedures:

select * from sys.all_objects where type='p' and is_ms_shipped=0
Kokoschka answered 12/10, 2012 at 11:28 Comment(1)
There is no reason to use sys.all_objects since you are filtering on is_ms_shipped=0. It can contain DDL Triggers, but those would be filtered out by type='p'. You might as well use sys.objects.Trapezius
B
4

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
Beller answered 20/10, 2008 at 19:26 Comment(1)
Why would you use this instead of sys.procedures where is_ms_shipped = 0? And why would you run a function objectproperty(object_id, N'IsMSShipped') for every row when there is a field is_ms_shipped that contains that value? Along those same lines, why run that function again when [type] IN ('P', 'PC') does the same thing? This method is unnecessarily complicated and inefficient.Trapezius
F
3

I've tweaked LostCajun's excellent post above to exclude system stored procedures. I also removed "Extract." from the code because I couldn't figure out what it's for and it gave me errors. The "fetch next" statement inside the loop also needed an "into" clause.

use <<databasename>>
go

declare @aQuery nvarchar(1024);
declare @spName nvarchar(64);
declare allSP cursor for
    select p.name  
    from sys.procedures p 
    where p.type_desc = 'SQL_STORED_PROCEDURE' 
    and LEFT(p.name,3) NOT IN ('sp_','xp_','ms_')
    order by p.name;
open allSP;
fetch next from allSP into @spName;
while (@@FETCH_STATUS = 0)
begin
    set @aQuery = 'sp_helptext [' + @spName + ']';
    exec sp_executesql @aQuery;
    fetch next from allSP into @spName;
end;
close allSP;
deallocate allSP;
Fruge answered 27/2, 2017 at 15:13 Comment(0)
G
2

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'
Garate answered 25/6, 2018 at 8:11 Comment(0)
J
1

I wrote this simple tsql to list the text of all stored procedures. Be sure to substitute your database name in field.

use << database name >>
go

declare @aQuery nvarchar(1024);
declare @spName nvarchar(64);
declare allSP cursor for
select p.name  from sys.procedures p where p.type_desc = 'SQL_STORED_PROCEDURE' order by p.name;
open allSP;
fetch next from allSP into @spName;
while (@@FETCH_STATUS = 0)
begin
    set @aQuery = 'sp_helptext [Extract.' + @spName + ']';
    exec sp_executesql @aQuery;
    fetch next from allSP;
end;
close allSP;
deallocate allSP;
Jilleen answered 23/12, 2014 at 15:19 Comment(1)
please see @Fruge 's rewrite of this.. which worked for me. This one didn't work as it had lot of errors.Holbrooke
T
1

This will returned all sp name

Select * 
FROM sys.procedures where [type] = 'P' 
     AND is_ms_shipped = 0 
     AND [name] not like 'sp[_]%diagram%'
Teahouse answered 8/1, 2015 at 14:29 Comment(2)
The condition on [type] should be [type] IN ('P', 'PC') else you are filtering out any CLR stored procs that are potentially there.Trapezius
At least this eliminates diagram procedures. better than others.Roar
H
1

This will give just the names of the stored procedures.

select specific_name
from information_schema.routines
where routine_type = 'PROCEDURE';
Horizon answered 16/10, 2016 at 19:55 Comment(0)
H
1

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'
Hardbitten answered 7/3, 2019 at 16:18 Comment(0)
N
0
select *  
  from dbo.sysobjects
 where xtype = 'P'
   and status > 0
Neurath answered 20/10, 2008 at 18:44 Comment(3)
status > 0 doesn't seem to differentiate between system stored procedures and those createdDomingo
Hmm. It does for us -- I don't know why.Neurath
Anyone using SQL Server 2005 or newer should move away from the dbo.sys* views. This query also filters out CLR stored procedures.Trapezius
H
0

This, list all things that you want

In Sql Server 2005, 2008, 2012 :

Use [YourDataBase]

EXEC sp_tables @table_type = "'PROCEDURE'" 
EXEC sp_tables @table_type = "'TABLE'"
EXEC sp_tables @table_type = "'VIEW'" 

OR

SELECT * FROM information_schema.tables
SELECT * FROM information_schema.VIEWS
Horseplay answered 4/12, 2012 at 8:9 Comment(1)
There is no reason to use, or benefit from using, sp_tables. Also, "PROCEDURE" is not a valid option for sp_tables. The only options for @table_type are: 'SYSTEM TABLE', 'TABLE', and 'VIEW'.Trapezius
K
0

Try this codeplex link, this utility help to localize all stored procedure from sql database.

https://exportmssqlproc.codeplex.com/

Knockout answered 21/3, 2017 at 4:55 Comment(0)
R
0
select * from DatabaseName.INFORMATION_SCHEMA.ROUTINES where routine_type = 'PROCEDURE'

select * from DatabaseName.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')
Rubicund answered 14/3, 2018 at 8:14 Comment(0)
P
0
USE DBNAME

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


GO 

This will work on mssql.

Punk answered 17/12, 2018 at 10:44 Comment(0)
S
0

Select list of stored procedure in SQL server. Refer here for more: https://coderrooms.blogspot.com/2017/06/select-list-of-stored-procedure-in-sql.html

Selfreliant answered 1/4, 2020 at 17:41 Comment(1)
Hello, and welcome. This code doesn’t appear to do what the title suggests. It appears to just create a stored procedure which returns a list of records from a PaymentDetails table. The OP wants a list of actual stored procedures.Plumy
W
0

exec sp_stored_procedures; Docs.Microsoft.com

Easy to remember.

Woodsum answered 3/2, 2021 at 2:57 Comment(0)
A
0

List All Store Procedures in SQL Server:

select * from sysobjects where type='P' order by name

List All Store Tables in SQL Server:

select * from sysobjects where type='U' order by name
Abnegate answered 8/8, 2023 at 11:6 Comment(1)
Hi. What does this answer add to all other answers on this post?Troyes

© 2022 - 2024 — McMap. All rights reserved.