### Error Handling in the SQL Code

Jun 29, 2020

### Error Handling in the SQL Code

Home – High on SQL!!!

I would suggest developer for coding in couple scenario in order to capture actual error while script deployment especially when we execute a query that modifies portion of data and fails. This requires redoing their works to clean it up when this happens. Also, it potentially breaks data integrity that damage the databases. In order to avoid that I always suggest developers to wrap up their SQL code using template like—   

BEGINBEGIN TRY
BEGIN TRAN

——————Add SQL Logic here—————–

COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
SELECT ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
END CATCH
END;

Cheers,
Zahid

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*