当前执行过程名称


Answers:



81
OBJECT_SCHEMA_NAME(@@PROCID) + '.' + OBJECT_NAME(@@PROCID)

3
如果在temp Proc中使用此函数,则无论是否检索模式名称,它都将返回NULL。第一个进程是“正常”,第二个BEGIN TRAN GO CREATE PROC utility.TempProc AS SELECT OBJECT_SCHEMA_NAME(@@PROCID)+'.'+OBJECT_NAME(@@PROCID) GO EXEC utility.TempProc GO ROLLBACK GO BEGIN TRAN GO CREATE PROC utility.#TempProc AS SELECT OBJECT_SCHEMA_NAME(@@PROCID)+'.'+OBJECT_NAME(@@PROCID) GO EXEC utility.#TempProc GO ROLLBACK GO
进程

15

您可以使用OBJECT_NAME(@@ PROCID)

返回当前Transact-SQL模块的对象标识符(ID)。Transact-SQL模块可以是存储过程,用户定义的函数或触发器。


6

在特定情况下,如果您对当前正在执行的临时存储过程的名称感兴趣,则可以通过以下方式获取它:

select name
from tempdb.sys.procedures
where object_id = @@procid

您不能在SQL Server中使用接受的答案来查找当前正在执行的临时存储过程的名称:

create procedure #p
as
select object_name(@@PROCID) as name
go
exec #p


name
--------------------------------------------------------------------------------------------------------------------------------
NULL

(1 row affected)

可以确认,在Enterprise 2014中以会话范围(双##)在2008R2兼容模式下进行了测试
Elaskanator

1
除了事实,这是正确的:谁来创建临时程序?:-D
Tarek Salha

0

您可以NULL在获取存储过程的模式和名称之前进行检查。

这意味着即使对于(全局)临时存储过程,您也可以获得正确的数据(单击图像放大):

非临时,临时和全局临时存储过程的名称

USE [master]; --so we can test temp sprocs without cheating by being in tempdb.
GO

BEGIN TRAN;
GO

CREATE PROC dbo.NotTempProc
AS
BEGIN
    SELECT CASE
        WHEN OBJECT_SCHEMA_NAME(@@PROCID) IS NULL
        THEN OBJECT_SCHEMA_NAME(@@PROCID, 2) + N'.' + OBJECT_NAME(@@PROCID, 2)
        ELSE OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID)
        END AS ProcName;
END
GO

EXEC dbo.NotTempProc;
GO

CREATE PROC dbo.#TempProc
AS
BEGIN
    SELECT CASE
        WHEN OBJECT_SCHEMA_NAME(@@PROCID) IS NULL
        THEN OBJECT_SCHEMA_NAME(@@PROCID, 2) + N'.' + OBJECT_NAME(@@PROCID, 2)
        ELSE OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID)
        END AS ProcName;
END
GO

EXEC dbo.#TempProc;
GO

CREATE PROC dbo.##GlobalTempProc
AS
BEGIN
    SELECT CASE
        WHEN OBJECT_SCHEMA_NAME(@@PROCID) IS NULL
        THEN OBJECT_SCHEMA_NAME(@@PROCID, 2) + N'.' + OBJECT_NAME(@@PROCID, 2)
        ELSE OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID)
        END AS ProcName;
END


GO

EXEC dbo.##GlobalTempProc;
GO

ROLLBACK;
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.