查询以列出所有存储过程

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

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

739774 次浏览
select *
from dbo.sysobjects
where xtype = 'P'
and status > 0
SELECT name,
type
FROM dbo.sysobjects
WHERE (type = 'P')

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

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

正如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_')

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

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

如果你使用的是SQL Server 2005,下面的操作可以正常工作:

select *
from sys.procedures
where is_ms_shipped = 0

这也可以帮助列出除了系统过程之外的过程:

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

这个,列出所有你想要的东西

在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'"

SELECT * FROM information_schema.tables
SELECT * FROM information_schema.VIEWS

下面将返回所选数据库中的所有过程

SELECT * FROM sys.procedures

我编写了这个简单的tsql来列出所有存储过程的文本。请确保在字段中替换数据库名称。

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;

这将返回所有sp名称

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 IN (
'P', -- stored procedures
'FN', -- scalar functions
'IF', -- inline table-valued functions
'TF' -- table-valued functions
)
ORDER BY type, name

这将只给出存储过程的名称。

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

您可以使用以下查询之一来查找一个数据库中的存储过程列表:

Query1:

    SELECT
*
FROM sys.procedures;

Query2:

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

如果你想找到所有数据库中的所有sp的列表,你可以使用下面的查询:

    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

我调整了上面LostCajun的优秀帖子,排除了系统存储过程。我还从代码中删除了“Extract.”,因为我不知道它是干什么用的,它给了我错误。循环中的“fetch next”语句还需要一个“into”子句。

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;

试试这个codeplex链接,这个实用程序帮助本地化sql数据库中的所有存储过程。

https://exportmssqlproc.codeplex.com/

选择“所有存储过程和视图”

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

只是名字:

SELECT SPECIFIC_NAME
FROM YOUR_DB_NAME.information_schema.routines
WHERE routine_type = 'PROCEDURE'
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')

获取对象的最佳方法是使用sys.sql_modules。您可以从这个表中找到所需的所有内容,并通过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'
USE DBNAME


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




GO

这将在mssql上工作。

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

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'

选择SQL server中的存储过程列表。更多信息请参考这里: https://coderrooms.blogspot.com/2017/06/select-list-of-stored-procedure-in-sql.html < / p >

< p > exec sp_stored_procedures; Docs.Microsoft.com < / p >

很容易记住。