BEGIN
TRANSACTION
UPDATE Inventory
SET UnitsInStock = 0
WHERE InvID = 4;
INSERT INTO InvHistory (InvID, ModifiedDate)
VALUES (4, GETDATE());
COMMIT TRANSACTION
If either DML statement fails, the entire transaction would be rolled back. DBA/Developer can also use the ROLLBACK statement and other procedural constructs within a block of Transact-SQL code to control when transactions are committed or rolled back. DBA/Developer might want to return a custom error message or perform other actions when specific statements are unsuccessful. To accomplish this, DBA/Developer would need to include additional error-handling code. For example, the following code would accomplish the same result, but DBA/Developer could include custom error processing within each BEGIN...END block:
BEGIN
TRANSACTION
UPDATE Inventory
SET UnitsInStock = 0
WHERE InvID = 4;
IF@@ERROR <> 0
BEGIN ROLLBACK
RETURN
END INSERT INTO InvHistory(InvID, ModifiedDate)
VALUES (4, GETDATE());
IF @@ERROR <> 0
BEGIN ROLLBACK
RETURN
END ELSE
COMMIT
DBA/Developer should not set the XACT_ABORT option to OFF and enclose the code within a single explicit transaction. With this setting, if one of the statements failed, only the failed statement would be rolled back, rather than the entire transaction. DBA/Developer should not issue the UPDATE and INSERT statements, then check @@ERROR and roll back the transaction if @@ERROR returns a non-zero value. The @@ERROR function returns a non-zero value, but only for the most recently executed statement. Therefore, DBA/Developer would have to check the value of @@ERROR after each DML statement. DBA/Developer should not include the code for both DML operations in a TRY block, and include a CATCH block. Although DBA/Developer can use a TRY...CATCH construct to customize error handling in a transaction, simply including the DML within a TRY block and including a CATCH block is not sufficient. DBA/Developer would need to include the appropriate code to roll back the transaction in the CATCH block if an error occurred.
No comments:
Post a Comment