This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing a small and useful demonstration for printing a Stored Procedure name in the CATCH block of SQL Server.
SQL Server developers are using TRY CATCH block in their Stored Procedures, but if they print Stored Procedure name in the CATCH block, it is easy to identify a particular stored procedure error from the error log files.
It is also beneficial for an application developer to determine the name of the stored procedure with an error.
We can use ERROR_PROCEDURE() to print a stored procedure name in the CATCH block.
Below is a small demonstration on this:
Create a sample stored procedure:
1 2 3 4 5 |
CREATE PROCEDURE usp_SampleErrorProc AS PRINT 'Sample message...' SELECT 1/0; GO |
Execute above sample stored procedure:
1 2 3 4 5 6 7 |
BEGIN TRY EXECUTE usp_SampleErrorProc END TRY BEGIN CATCH SELECT 'Procedure_Name: ' + ERROR_PROCEDURE() AS ErrorProcedureName; END CATCH; GO |
Leave a Reply