当前执行过程名称

是否可以在 MSSQLServer 中获取当前存储过程的名称?

也许有一个系统变量或函数像 GETDATE()

87783 次浏览

You may try this:

SELECT OBJECT_NAME(@@PROCID)

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

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.

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

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)

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

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;

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