ERROR_PROCEDURE() returns null – Fixed – SQL Server

If you have ever handled errors by using TRY-CATCH in SQL Server then you must be familiar with a function called ERROR_PROCEDURE(), which returns the stored procedure name when any error occurs during execution, and by using this function we write the procedure name with the error into our log table or event viewer for future review. But the ERROR_PROCEDURE() function does not do well with the dynamic SQL statements. If there are any dynamic SQL statements being executed inside the procedure then this function returns NULL instead of actual procedure name. This is annoying and I have no clue on this.

Interview Question: How to get the stored procedure name inside the stored procedure?
The answer is in the CATCH block.

There are two approaches and are mentioned in the catch block.

sp-name-inside-the-sp-sql-server

Prasad Sahoo

Prasad Sahoo, is an enthusiast of Microsoft technologies in general and a passionate database professional, mainly focusing on SQL Server performance tuning and business intelligence. He has contributed a long time of his career working on SQL Server and other RDBMS. If he is not busy with SQL stuff then he must be watching some Hollywood movies.

One thought on “ERROR_PROCEDURE() returns null – Fixed – SQL Server

Leave a Reply

%d bloggers like this: