Query to list all stored procedures

2019-01-07 02:00发布

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.

22条回答
疯言疯语
2楼-- · 2019-01-07 02:38

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
查看更多
女痞
3楼-- · 2019-01-07 02:39

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;
查看更多
The star\"
4楼-- · 2019-01-07 02:39
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')
查看更多
神经病院院长
5楼-- · 2019-01-07 02:41

This will give just the names of the stored procedures.

select specific_name
from information_schema.routines
where routine_type = 'PROCEDURE';
查看更多
▲ chillily
6楼-- · 2019-01-07 02:43

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
查看更多
在下西门庆
7楼-- · 2019-01-07 02:43

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_')
查看更多
登录 后发表回答