DECLARE @err int UPDATE cart SET ..... SET @err = @@ERROR IF @err != 0 GOTO __cleanup UPDATE nextTable SET ... SET @err = @@ERROR IF @err != 0 GOTO __cleanup __cleanup: IF @err != 0 BEGIN -- cleanup code goes here ENDor something like that
>-- Code from Mike Levy >IF EXISTS (SELECT CartID FROM cart WHERE UserID = @tcUserID) > BEGIN > UPDATE cart > SET @CartID = CartID, > LastModTs = CURRENT_TIMESTAMP > WHERE UserID = @tcUserID > IF @@ERROR <> 0 > BEGIN > ROLLBACK TRAN > PRINT 'ERROR: An error occurred while attempting to update record to the Cart Table.' > RETURN 4 > END > END >ELSE >-- New User > BEGIN > INSERT INTO cart (UserID) values (@tcUserID) > > SELECT @CartID = SCOPE_IDENTITY() > IF @@ERROR <> 0 > BEGIN > ROLLBACK TRAN > PRINT 'ERROR: An error occurred while attempting to insert record to the Cart Table.' > RETURN 5 > END > END?
>USE pubs >GO >DECLARE @del_error int, @ins_error int >-- Start a transaction. >BEGIN TRAN > >-- Execute the DELETE statement. >DELETE authors >WHERE au_id = '409-56-7088' > >-- Set a variable to the error value for >-- the DELETE statement. >SELECT @del_error = @@ERROR > >-- Execute the INSERT statement. >INSERT authors > VALUES('409-56-7008', 'Bennet', 'Abraham', '415 658-9932', > '6223 Bateman St.', 'Berkeley', 'CA', '94705', 1) >-- Set a variable to the error value for >-- the INSERT statement. >SELECT @ins_error = @@ERROR > >-- Test the error values. >IF @del_error = 0 AND @ins_error = 0 >BEGIN > -- Success. Commit the transaction. > PRINT "The author information has been replaced" > COMMIT TRAN >END >ELSE >BEGIN > -- An error occurred. Indicate which operation(s) failed > -- and roll back the transaction. > IF @del_error <> 0 > PRINT "An error occurred during execution of the DELETE > statement." > > IF @ins_error <> 0 > PRINT "An error occurred during execution of the INSERT > statement." > > ROLLBACK TRAN >END>