[sql-server] Query to list all stored procedures

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.

This question is related to sql-server tsql

The answer is


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


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')

This will returned all sp name

Select * 
FROM sys.procedures where [type] = 'P' 
     AND is_ms_shipped = 0 
     AND [name] not like 'sp[_]%diagram%'

SELECT name, 
       type
  FROM dbo.sysobjects
 WHERE (type = 'P')

The following will Return All Procedures in selected database

SELECT * FROM sys.procedures

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

select * 
  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'

Just the names:

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

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

select *  
  from dbo.sysobjects
 where xtype = 'P'
   and status > 0

SELECT name, 
       type
  FROM dbo.sysobjects
 WHERE (type = 'P')

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

https://exportmssqlproc.codeplex.com/


SELECT name, 
       type
  FROM dbo.sysobjects
 WHERE (type = 'P')

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'

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;

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

select *
  from sys.procedures
 where is_ms_shipped = 0

exec sp_stored_procedures; Docs.Microsoft.com

Easy to remember.


SELECT name, 
       type
  FROM dbo.sysobjects
 WHERE (type = 'P')

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

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;

This will give just the names of the stored procedures.

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

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

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

https://exportmssqlproc.codeplex.com/


Select All Stored Procedures and Views

select name,type,type_desc
from sys.objects
where type in ('V','P')
order by name,type

select *  
  from dbo.sysobjects
 where xtype = 'P'
   and status > 0

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

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

select *
  from sys.procedures
 where is_ms_shipped = 0

exec sp_stored_procedures; Docs.Microsoft.com

Easy to remember.


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

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

Select All Stored Procedures and Views

select name,type,type_desc
from sys.objects
where type in ('V','P')
order by name,type

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

The following will Return All Procedures in selected database

SELECT * FROM sys.procedures

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;

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

select *  
  from dbo.sysobjects
 where xtype = 'P'
   and status > 0

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

select * from sys.all_objects where type='p' and is_ms_shipped=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

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')

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;

USE DBNAME

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


GO 

This will work on mssql.


This will give just the names of the stored procedures.

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

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

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'

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

USE DBNAME

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


GO 

This will work on mssql.


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

select * from sys.all_objects where type='p' and is_ms_shipped=0

Just the names:

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

This will returned all sp name

Select * 
FROM sys.procedures where [type] = 'P' 
     AND is_ms_shipped = 0 
     AND [name] not like 'sp[_]%diagram%'

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

select *  
  from dbo.sysobjects
 where xtype = 'P'
   and status > 0

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'

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

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