Skip to content

Instantly share code, notes, and snippets.

SQL try catch transaction block template
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