Skip to content

Instantly share code, notes, and snippets.

@umair-me
Last active May 4, 2018 10:52
Show Gist options
  • Save umair-me/fd41b19ccbc5407ec63c to your computer and use it in GitHub Desktop.
Save umair-me/fd41b19ccbc5407ec63c to your computer and use it in GitHub Desktop.
Run query in transaction with error logs
BEGIN TRANSACTION;
BEGIN TRY
-- ADD SQL QUERIES HERE
END TRY
BEGIN CATCH
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;
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