SAVEPOINT

The SAVEPOINT Statement

A SAVEPOINT marks a point inside a transaction. ROLLBACK TO savepoint undoes only the work done after that point, and the transaction continues.

SAVEPOINT savepoint_name;
ROLLBACK TO savepoint_name;       -- SQL Server: ROLLBACK TRANSACTION savepoint_name
RELEASE SAVEPOINT savepoint_name; -- forget the savepoint (not in SQL Server)

Example

Example

Keep the first insert, undo the second:

BEGIN TRANSACTION;
INSERT INTO Customers (CustomerID, CustomerName, Country) VALUES (20, 'Kite Foods', 'India');
SAVEPOINT after_first;
INSERT INTO Customers (CustomerID, CustomerName, Country) VALUES (21, 'Typo Customer', 'Nowhere');
ROLLBACK TO after_first;
COMMIT;

SELECT CustomerID, CustomerName FROM Customers WHERE CustomerID >= 20;

Result: 1 row

CustomerIDCustomerName
20Kite Foods

Learn More

Tip: ACID properties, isolation levels, locking and deadlocks are covered in the Advance level, Transactions module.