Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQL Server ERROR_PROCEDURE(): How to Use It

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

ERROR_PROCEDURE() returns the name of the stored procedure or trigger where an error occurred, and it only returns a value inside a CATCH block. To use it, wrap the risky statement in BEGIN TRY...END CATCH, then select ERROR_PROCEDURE() alongside ERROR_NUMBER(), ERROR_MESSAGE(), ERROR_LINE() and ERROR_SEVERITY() so your error log records which procedure failed, not just what failed. The function takes no arguments and returns nvarchar(128); it returns NULL when the error did not happen inside a procedure or trigger, or when you call it outside a CATCH block.

What You'll Need

  • SQL Server 2005 or later (TRY...CATCH and the ERROR_ functions were introduced in SQL Server 2005).
  • Permission to create or alter the procedure you are testing, plus EXECUTE permission on it.
  • A client such as SQL Server Management Studio (SSMS) or Azure Data Studio connected to the instance.
  • A test database where you can safely create a sample table and procedure.

Step-by-Step

  1. Confirm where the error functions are legal. ERROR_PROCEDURE() is only meaningful inside a CATCH block. If you run SELECT ERROR_PROCEDURE(); in a normal query window you will get NULL, and that is expected, not a bug. Plan to call it from a CATCH handler.
  2. Create a small test object. In a scratch database, run something like:
    CREATE TABLE dbo.TestTable (ID int NOT NULL PRIMARY KEY);
    CREATE PROCEDURE dbo.usp_TestError AS BEGIN INSERT dbo.TestTable (ID) VALUES (1); INSERT dbo.TestTable (ID) VALUES (1); END;
    The second insert violates the primary key, which gives you a predictable error to capture.
  3. Wrap the call in TRY...CATCH. In a new query window:
    BEGIN TRY
      EXEC dbo.usp_TestError;
    END TRY
    BEGIN CATCH
      SELECT ERROR_PROCEDURE() AS ErrorProcedure, ERROR_NUMBER() AS ErrorNumber, ERROR_MESSAGE() AS ErrorMessage, ERROR_LINE() AS ErrorLine;
    END CATCH;
  4. Run it and read the result. ErrorProcedure should show usp_TestError, ErrorNumber should be 2627 (primary key violation) and ErrorLine should point at the failing INSERT. If ErrorProcedure is NULL, the error was raised outside a procedure or trigger, or the CATCH did not actually catch it.
  5. Add the function to your real error logging. Inside the CATCH block of production procedures, insert the ERROR_ functions into a logging table, for example INSERT dbo.ErrorLog (ErrorTime, ErrorProcedure, ErrorNumber, ErrorMessage, ErrorLine) VALUES (SYSDATETIME(), ERROR_PROCEDURE(), ERROR_NUMBER(), ERROR_MESSAGE(), ERROR_LINE());. Keep the INSERT itself inside its own TRY...CATCH or use a separate connection so a logging failure does not hide the original error.
  6. Test nested procedures. If procedure A calls procedure B and B fails, ERROR_PROCEDURE() in A's CATCH block reports the procedure where the error was actually raised. Create a wrapper procedure that calls usp_TestError and confirm the value you get, so you know how your logs will read.
  7. Check the scope rule. The ERROR_ functions return information only for the CATCH block that is currently executing. If you catch an error, then call another procedure that fails and catches it, the outer CATCH still sees the original error values. Capture the values into variables or a table immediately if you need them later.

Common Mistakes

  • Calling ERROR_PROCEDURE() outside a CATCH block. It returns NULL, which is often mistaken for "the error came from ad hoc SQL". It simply has no context.
  • Expecting the outer procedure name. The function reports where the error was raised, which may be a nested procedure or trigger, not the procedure you called from the application.
  • Assuming it works for all errors. Errors with severity 20 or higher that terminate the connection, and some compilation errors, never reach the CATCH block, so nothing is logged.
  • Losing the values by running more statements first. The ERROR_ functions reflect the current CATCH context; capture them into variables before doing other work if you need to use them later.
  • Forgetting that triggers count. An error inside a trigger reports the trigger name, which can confuse people who only expected procedure names.

Tips

  • Log ERROR_PROCEDURE() together with ERROR_NUMBER(), ERROR_SEVERITY(), ERROR_STATE(), ERROR_LINE() and ERROR_MESSAGE(); the procedure name alone rarely tells the full story.
  • Use ERROR_PROCEDURE() in a reusable logging procedure that every CATCH block calls, so the format is consistent across the database.
  • Remember the return type is nvarchar(128); if you store it in a column, size it accordingly and allow NULL.
  • Pair it with ERROR_MESSAGE() when searching logs, because the same error number can appear in many procedures.
  • In Azure SQL Database and Azure SQL Managed Instance the behaviour is the same as on-premises SQL Server; the TRY...CATCH rules do not change.

Troubleshooting

If ERROR_PROCEDURE() returns NULL, first confirm you are inside a CATCH block and that the error was actually raised by a stored procedure or trigger. Errors raised by dynamic SQL executed with sp_executesql or EXEC() can report the calling context rather than a procedure name, so test with a direct procedure call to compare. If the CATCH block never runs, check that the statement is inside BEGIN TRY...END TRY and that the error severity is low enough to be catchable; connection-terminating errors and some compile errors bypass TRY...CATCH entirely.

If the procedure name looks truncated, check the column you are inserting into; nvarchar(128) is the maximum the function returns, so a smaller column will silently cut it. If you see the wrong procedure name in nested calls, that is the documented behaviour: the function reports the scope where the error occurred. Capture it in the innermost CATCH if you need the innermost name, or let it bubble up if you want the outermost handler to log it. If logging itself fails, wrap the logging INSERT in its own TRY...CATCH and write to a fallback table or file so the original error is not lost.

Frequently Asked Questions

What does ERROR_PROCEDURE() return?

It returns the name of the stored procedure or trigger in which the error occurred, as nvarchar(128), or NULL if the error did not occur in a procedure or trigger or the call is outside a CATCH block.

Does it work outside a CATCH block?

No. Outside a CATCH block it returns NULL. The ERROR_ functions are only populated while a CATCH block is executing.

Does it return the outer or inner procedure name?

It returns the procedure where the error was raised. In nested calls that is usually the innermost procedure or trigger, not the one the application called.

Can I use it in a trigger?

Yes. If the error occurs inside a trigger, ERROR_PROCEDURE() reports the trigger name when called from a CATCH block that handles the error.

Why is it NULL for errors from dynamic SQL?

Errors raised inside dynamic SQL executed with sp_executesql or EXEC() may not be associated with a named procedure, so the function can return NULL. Test your specific pattern to see what your version reports.

Is it available in Azure SQL?

Yes. ERROR_PROCEDURE() and the other ERROR_ functions are supported in Azure SQL Database and Azure SQL Managed Instance with the same TRY...CATCH behaviour as SQL Server.