SQL Server 当前执行过程名称

6jygbczu  于 2023-01-08  发布在  其他
关注(0)|答案(6)|浏览(126)

是否可以获得MS SQL Server中当前存储过程的名称?
也许有一个系统变量或函数,如GETDATE()

guicsvcw

guicsvcw1#

你可以试试这个:

SELECT OBJECT_NAME(@@PROCID)
    • 更新:**此命令在SQL Server 2016上仍然有效。
wooyq4lh

wooyq4lh2#

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

niwlg2el3#

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

px9o7tmv

px9o7tmv4#

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

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

e4yzc0pl5#

在获取存储过程的架构和名称之前,可以检查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;
ogsagwnx

ogsagwnx6#

我知道这是旧的,但这是我用的。它似乎总是有效的。

BEGIN TRAN
GO
-- Stored procedure, function of trigger
CREATE PROC dbo.TempProc AS
    DECLARE @DATETIME = GETDATE()
        ,@Me VARCHAR(64) = COALESCE (
             OBJECT_SCHEMA_NAME(@@PROCID, DB_ID())
            ,OBJECT_SCHEMA_NAME(@@PROCID, DB_ID('tempdb'))
            ,'session'
        )
        + '.'
        + COALESCE (
             OBJECT_NAME(@@PROCID, DB_ID())
            ,OBJECT_NAME(@@PROCID, DB_ID('tempdb'))
            ,'SQL'
        )

    SELECT ProcName = @Me
GO
EXEC dbo.TempProc
GO
ROLLBACK
GO
BEGIN TRAN
GO
-- Temp Stored procedure
CREATE PROC #TempProc AS
    DECLARE @DATETIME = GETDATE()
        ,@Me VARCHAR(64) = COALESCE (
             OBJECT_SCHEMA_NAME(@@PROCID, DB_ID())
            ,OBJECT_SCHEMA_NAME(@@PROCID, DB_ID('tempdb'))
            ,'session'
        )
        + '.'
        + COALESCE (
             OBJECT_NAME(@@PROCID, DB_ID())
            ,OBJECT_NAME(@@PROCID, DB_ID('tempdb'))
            ,'SQL'
        )

    SELECT ProcName = @Me
GO
EXEC #TempProc 
GO
ROLLBACK
GO
-- SSMS or direct SQL statement
DECLARE @DATETIME = GETDATE()
    ,@Me VARCHAR(64) = COALESCE (
         OBJECT_SCHEMA_NAME(@@PROCID, DB_ID())
        ,OBJECT_SCHEMA_NAME(@@PROCID, DB_ID('tempdb'))
        ,'session'
    )
    + '.'
    + COALESCE (
         OBJECT_NAME(@@PROCID, DB_ID())
        ,OBJECT_NAME(@@PROCID, DB_ID('tempdb'))
        ,'SQL'
    )
SELECT ProcName = @Me

相关问题