Skip to content

Instantly share code, notes, and snippets.

@dantuck
Created September 16, 2011 14:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dantuck/1222277 to your computer and use it in GitHub Desktop.
Save dantuck/1222277 to your computer and use it in GitHub Desktop.
TSQL Transaction Management
DECLARE @ERRORCODE INT
BEGIN TRANSACTION
-- do action
-- check for errors after each transaction if desired
SELECT @ERRORCODE = @@ERROR
IF (@ERRORCODE <> 0) GOTO PROBLEM
-- last statement before Problem resolution
COMMIT TRANSACTION
PROBLEM:
IF(@ERRORCODE <> 0 ) BEGIN
ROLLBACK TRANSACTION
-- Raise an error and return
RAISERROR ('Put your error response here', 16, 1)
RETURN
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment