114

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()?

Oreo
  • 529
  • 3
  • 16
Sergey Metlov
  • 25,747
  • 28
  • 93
  • 153

6 Answers6

169

You may try this:

SELECT OBJECT_NAME(@@PROCID)

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

Alireza Maddah
  • 5,718
  • 2
  • 21
  • 25
92
OBJECT_SCHEMA_NAME(@@PROCID) + '.' + OBJECT_NAME(@@PROCID)
RAS
  • 8,100
  • 16
  • 64
  • 86
karthik
  • 921
  • 6
  • 2
  • 3
    If 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` – SAinCA Aug 04 '16 at 22:14
  • See my answer below for session/global temp procedures ^_^ – Oreo Oct 27 '22 at 23:16
16

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.

Amit
  • 21,570
  • 27
  • 74
  • 94
10

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)
Oreo
  • 529
  • 3
  • 16
ajeh
  • 2,652
  • 2
  • 34
  • 65
2

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;
Oreo
  • 529
  • 3
  • 16
  • 1
    While you're likely to know whether the stored proc is a temp stored proc or not when you're writing the code for it I think this answer shows clearly the difference between the code you'd need to add to a temp stored proc vs a normal stored proc. – Simon Elms Oct 26 '22 at 22:50
2

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
Oreo
  • 529
  • 3
  • 16
Gord Gray
  • 23
  • 4