当前执行的程序名称

发布于 2024-11-08 00:27:12 字数 86 浏览 0 评论 0原文

是否可以获取 MS SQL Server 中当前存储过程的名称?

也许有像 GETDATE() 这样的系统变量或函数?

Is it possible to get the name of the current Stored Procedure in MS SQL Server?

Maybe there is a system variable or function like GETDATE()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

屌丝范 2024-11-15 00:27:12

您可以尝试以下操作:

SELECT OBJECT_NAME(@@PROCID)

更新:此命令在 SQL Server 2022 上仍然有效。

You may try this:

SELECT OBJECT_NAME(@@PROCID)

Update: This command is still valid on SQL Server 2022.

只是一片海 2024-11-15 00:27:12
OBJECT_SCHEMA_NAME(@@PROCID) + '.' + OBJECT_NAME(@@PROCID)
OBJECT_SCHEMA_NAME(@@PROCID) + '.' + OBJECT_NAME(@@PROCID)
后eg是否自 2024-11-15 00:27:12

您可以使用OBJECT_NAME(@@PROCID)

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

You can use OBJECT_NAME(@@PROCID)

Returns the object identifier (ID) of
the current Transact-SQL module. A
Transact-SQL module can be a stored
procedure, user-defined function, or
trigger.

像极了他 2024-11-15 00:27:12

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

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)

In the specific case where you are interested in the name of the currently executing temporary stored procedure, you can get it via:

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

You cannot use the accepted answer in SQL Server to find the name of the currently executing temporary stored procedure:

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


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

(1 row affected)
等风也等你 2024-11-15 00:27:12

您可以在获取存储过程的架构和名称之前检查 NULL 架构:

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;

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

姓名非临时、临时和全局临时存储过程

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;

You can check for a NULL schema before getting the schema and name of the stored procedure:

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;

This means that you can get the right data even for (global) temporary stored procedures (click image to make bigger):

names of non-temporary, temporary, and global temporary stored procedures

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;
我纯我任性 2024-11-15 00:27:12

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

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

I know this is old, but this is what I use. It appears to always work.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文