How to continue executing a stored procedure after error is logged in the CATCH block

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to continue executing a stored procedure after error is logged in the CATCH block



I've a requirement when a record fails to insert a record into the table, error should be caught in the CATCH block and stored procedure should not stop because of that error.



Below is the scenario:


CREATE PROCEDURE [dbo].[Pkg_StoredProcedure]
@Parameter1 NVARCHAR(20),
@Parameter2 INT,
@Parameter3 INT
AS
SET NOCOUNT ON;

BEGIN TRY
BEGIN TRAN T1
INSERT INTO Table1
SELECT * FROM Table2

COMMIT TRAN T1

END TRY
BEGIN CATCH

ROLLBACK TRAN T1

DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;

SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();

RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);

INSERT INTO [ETL_ErrorLog]
SELECT DISTINCT
column1, column2, column3, column4,
@ErrorMessage + '(' + CONVERT(VARCHAR, @ErrorSeverity ) + '/' +
CONVERT (VARCHAR, @ErrorState)+ ')' as 'ErrorMessage'

END CATCH
GO





Place the RAISERROR as the last statement
– Afshin Amiri
Aug 12 at 6:58





Did the above suggested but still fails. I tried by Uncommenting the RAISERROR CATCHBLOCK as shown in th eexample above still keeping the etl_Errorlog table . stored procedure executed successfully by logging the error in the ETL.Errorlog. Is this the best practice?
– Sujatha
Aug 12 at 13:14





"error should be caught in the CATCH block and stored procedure should not stop because of that error." what other work need to perform after error.what is the other query in your store procedure ? what code should not stop after error ?
– KumarHarsh
Aug 13 at 9:58




2 Answers
2



In a scenario like this I would try to separate out the code, so that when the error occurs the entire process isn't interrupted



In other words the code:


INSERT INTO Table1
SELECT * FROM Table2

COMMIT TRAN T1



I'm assuming this is not the actual code?
Could you move this section, into another stored procedure, (with the try catch) and run a loop that calls this new procedure? Then when the error occurs the "called" procedure will fail but not the "calling" procedure.
Error will be logged, loop continues



The try / catch construct is there to allow you control on exceptions instead of the default exit handler (meaning, abrupt procedure termination).


try / catch



Whatever code is after the END CATCH would be executed if you are not exiting the procedure from within the try / catch blocks.


END CATCH


try / catch



Note that in the example above you are triggering the error handling mechanism BEFORE the insert. As such, I suspect you are not seeing anything in your table [ELT_ErrorLog].


[ELT_ErrorLog]





when a record fails to insert it is logging into the ETL_ErrorLog table in the CATCH block and fails.
– Sujatha
Aug 12 at 12:32





It does not fail. The invocation of RAISERROR is what makes it exit the procedure. Comment it, add a print 'Hi there' immediately after the END CATCH and see if you get the printout (you definitely should!!).
– FDavidov
Aug 12 at 12:37



RAISERROR


print 'Hi there'


END CATCH





Tried the suggested with print out but when stored procedure is executed it shows both error message and print out message and logged a record into the Error Log as shown in the example.
– Sujatha
Aug 12 at 13:38





this is what i want ...when the stored procedure is executed it should run successfully and log the error in the table. for that purpose when RAISEERROR in the CATCH BLOCK is removed in the above code noticed stored procedure executed successfully by logging the error in the error_log table.Can you please let me know Is this the best practice.
– Sujatha
Aug 12 at 13:39






As stated, the purpose of try-catch blocks is to allow you to define NON-DEFAULT exception handling. In such case, you don't need the RAISERROR at all.
– FDavidov
Aug 12 at 14:55


try-catch


RAISERROR






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard