什么查询可以返回SQL Server数据库中所有存储过程的名称

如果查询可以排除系统存储过程,那将更有帮助。


当前回答

根据我的理解,“首选”方法是使用information_schema表:

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

其他回答

这将返回所有sp名称

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

正如Mike所说,最好的方法是使用information_schema。只要不在主数据库中,系统存储过程就不会返回。

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

如果出于某种原因,主数据库中有非系统存储过程,可以使用查询(这会过滤掉大多数系统存储过程):

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

这将显示所有的存储过程和代码:

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 

这将在mssql上工作。

不幸的是,INFORMATION_SCHEMA不包含系统进程的信息。

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