Current executing procedure name

2019-01-31 04:55发布

问题:

Is it possible to get the name of current Stored Procedure in MS SQL Server? May be there is any system variable or function like GETDATE() ?

回答1:

You may try this:

SELECT OBJECT_NAME(@@PROCID)

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



回答2:

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


回答3:

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.



回答4:

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)