Created
October 22, 2024 14:42
-
-
Save cemerson/09a4b1e33d38a62232318d944a8cc1c6 to your computer and use it in GitHub Desktop.
SQL try catch transaction block template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BEGIN TRY | |
BEGIN TRANSACTION | |
-- DO STUFF | |
ROLLBACK TRANSACTION --ROLLBACK COMMIT | |
END TRY | |
BEGIN CATCH | |
IF @@TRANCOUNT > 0 | |
BEGIN | |
ROLLBACK TRANSACTION | |
END | |
DECLARE @ErrorMessage NVARCHAR(4000) | |
DECLARE @ErrorSeverity INT | |
DECLARE @ErrorState INT | |
SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(),@ErrorState = ERROR_STATE() | |
RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState) | |
END CATCH |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment