Entering content frame

SAVEPOINT/ROLLBACK TO Statement (savepoint/rollback_to_statement) Locate the document in the library structure

Within a transaction, subtransactions can be defined to make a series of database operations within a transaction appear as a unit with regard to modifications to the database.

The SAVEPOINT statement is used to define an SQL savepoint – and therefore the start of a subtransaction within a transaction - and to assign this savepoint a name. A subsequent ROLLBACK TO statement with the SQL savepoint name reverses any modifications that have been made in the meantime, without affecting the database operations that were executed within the transaction before the start of this subtransaction.

Caution

An SQL savepoint defined by a SAVEPOINT statement is not a savepoint.

Syntax

<savepoint_statement> ::= SAVEPOINT <sql_savepoint_name>

<rollback_to_statement> ::= ROLLBACK TO [SAVEPOINT] <sql_savepoint_name>

<sql_savepoint_name> ::= <identifier>

Explanation

SAVEPOINT Statement

The SAVEPOINT statement opens a subtransaction. This means the database system records the current position (SQL savepoint) in the transaction and assigns it the name SQL_savepoint_name. The SQL savepoint is identified as active. Any sequence of SQL statements can then follow. This sequence of SQL statements may contain additional SAVEPOINT statements.

Names of SQL savepoint in a transaction must be different. If an SQL savepoint name is assigned twice within a transaction, the SQL savepoint in this transaction defined by the first SAVEPOINT statement becomes inactive.

ROLLBACK TO Statement

A ROLLBACK TO statement reverses all database modifications made in the active transaction following the SAVEPOINT statement.

The SQL savepoint specified in the ROLLBACK TO statement must be an active SQL statement in the transaction. All SQL savepoints created after this SQL savepoint are inactive. All SQL savepoints created before this SQL savepoint remain active.

The specified SQL savepoint also remains active after the ROLLBACK TO statement has been executed. This means the ROLLBACK TO statement can be executed in the same transaction more than once by specifying the same SQL savepoint name.

Additional Information

The SAVEPOINT/ROLLBACK TO statement does not affect locks assigned to the transaction. In particular, these SQL statements do not release any locks. Locks are only released by COMMIT or ROLLBACK.

Subtransactions are particularly useful for keeping the effects of database procedures atomic in the sense that they either fulfill all their tasks or else have no effect. To this end, a SAVEPOINT statement is specified initially. If the subroutine could not fulfill its tasks, a ROLLBACK TO statement is used to reverse all the modifications made by the database procedure.

The COMMIT statement and the ROLLBACK statement close any open subtransactions implicitly.

See also:

Transactions

SUBTRANS Statement (subtrans_statement)

 

Leaving content frame