ROLLBACK TO SAVEPOINT

This MySQL statement instructs the server to reverse SQL statements for the current transaction back to a point marked in the transaction by the SAVEPOINT statement.

Syntax

Below is the syntax for this command. Text within square brackets (i.e, [ and ] ) are optional. Choices are separated by bars (i.e, | ). Ellipses preceded by a comma indicates a repeating pattern. Ellipses before or after syntax are used to highlight a relevant except from the larger syntax. Text displayed in italic letters represent text that should be replaced with the specific names related to the database (e.g., column with the name of the column).

ROLLBACK TO SAVEPOINT identifier

Explanation

This MySQL statement instructs the MySQL server to reverse SQL statements for the current transaction back to a point marked in the transaction by the MySQL statement, SAVEPOINT. Any transactions for the session made after the savepoint are undone. This is in contrast to the MySQL statement, ROLLBACK by itself, which undoes all changes since the start of the transaction. Transaction statements are currently supported by the InnoDB, NDB Cluster, and BDB storage engines and are ignored if used with MyISAM tables. Multple savepoints may be set up during a transaction.

Examples

Below is an example of this MySQL statement:

START TRANSACTION;

LOCK TABLES orders WRITE;

INSERT DATA INFILE '/tmp/customer_info.sql'
INTO TABLE orders;

SAVEPOINT orders_import;

INSERT DATA INFILE '/tmp/customer_orders.sql'
INTO TABLE orders;

SELECT...

SAVEPOINT orders_import1;

INSERT DATA INFILE '/tmp/customer_orders1.sql'
INTO TABLE orders;

SELECT...

ROLLBACK TO SAVEPOINT orders_import1;

In this example, the database administrator has imported a customer information file and two files containing customer orders and has set up two savepoints. After running a few SELECT statements (not fully shown here), he decides that there was a problem loading the second batch of orders, so he rollsback the transaction to the savepoint, eliminating the data that was imported from the customer_orders1.sql file. If he wants, he can still rollback all of the orders imported, as well as the whole transaction. When he's finished, he can commit the transactions by executing the MySQL statement, COMMIT. See the description of that statement in our MySQL documentation for more information on committing transactions explicitly and implicitly.