Skip to content

Instantly share code, notes, and snippets.

@mhinton
Created December 18, 2019 00:28
Show Gist options
  • Save mhinton/3465c6a06aa05761cf25085ca2b251cb to your computer and use it in GitHub Desktop.
Save mhinton/3465c6a06aa05761cf25085ca2b251cb to your computer and use it in GitHub Desktop.
SQL Server Try Catch
BEGIN TRANSACTION;
BEGIN TRY
-- sql commands here
END TRY
BEGIN CATCH
-- Use RAISERROR inside the CATCH block to return error
-- information about the original error that caused
-- execution to jump to the CATCH block.
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;
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION; --RollBack in case of Error
END CATCH;
IF @@TRANCOUNT > 0
COMMIT TRANSACTION;
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment