Created
December 18, 2019 00:28
-
-
Save mhinton/3465c6a06aa05761cf25085ca2b251cb to your computer and use it in GitHub Desktop.
SQL Server Try Catch
This file contains 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 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