Current executing procedure name
82,580
Solution 1
You may try this:
SELECT OBJECT_NAME(@@PROCID)
Update: This command is still valid on SQL Server 2016.
Solution 2
OBJECT_SCHEMA_NAME(@@PROCID) + '.' + OBJECT_NAME(@@PROCID)
Solution 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.
Solution 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)
Solution 5
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
Related videos on Youtube
Comments
-
Sergey Metlov almost 3 years
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()
? -
Buggieboy about 9 yearsIt's worth noting that the returned value is of type SYSNAME.
-
Vinay Sinha over 8 yearswhat to do for function not procedure? any idea? Please help
-
Pimenta about 8 yearsStill valid on SQL Serve 2012
-
Fka almost 8 yearsStill valid on SQL Server 2016
-
SAinCA almost 8 yearsIf you use this inside a temp Proc, it returns NULL, with or without the schema name retrieval. 1st proc is "normal", 2nd is temp, in this code:
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
-
ajeh over 7 yearsDoes not work for the session or global temporary stored procedures.
-
Elaskanator about 6 years@ajeh No, I get a random table name when I try this from a temporary stored procedure.
-
ajeh about 6 years@Elaskanator Provide your SQL like I did in the updated answer, we'll figure out what is going on. And your server version too, please. ATM I have access to SQL 2012 and 2016 and in both the result is NULL
-
Elaskanator about 6 yearsCan confirm, tested in Enterprise 2014 in 2008R2 compatibility mode with session-scoping (double ##)
-
Tarek Salha over 4 yearsbesides the fact, that it is right: Who would create temporary procedures?? :-D
-
Bill Roberts almost 3 years@Buggieboy Please elaborate: why is it worth noting the type returned is SYSNAME?