SQL Server TRANSACTION with SAVE
SQL Server TRANSACTION with SAVE is a SQL statement in the Transactions category. SQL Server: SAVE TRANSACTION creates a marker within a transaction for partial rollback. The syntax is SAVE TRANSACTION savepoint_name;. It returns save transaction. A typical example: BEGIN TRANSACTION; INSERT INTO log VALUES ('Step 1'); SAVE TRANSACTION sp1; INSERT INTO log VALUES ('Step 2'); UPDATE accounts SET balance = balance - 100 WHERE id = 1; IF (SELECT balance FROM accounts WHERE id = 1) < 0 BEGIN ROLLBACK TRANSACTION sp1; -- Undo Step 2 only PRINT 'Rolled back Step 2 - insufficient funds'; END INSERT INTO log VALUES ('Step 3'); COMMIT TRANSACTION; -- Step 1 and… A close relative is BEGIN / START TRANSACTION, which begins a new transaction. Changes made within are invisible to others until COMMIT. A close relative is COMMIT, which saves all changes made in the current transaction permanently. A close relative is ROLLBACK, which undoes all changes made in the current transaction (or to a savepoint).