The SUBTRANS statement (subtrans_statement) divides a transaction into units known as subtransactions.
<subtrans_statement> ::= SUBTRANS BEGIN | SUBTRANS END | SUBTRANS ROLLBACK
A subtransaction is opened, that is, the database system records the current point in the transaction. This can be followed by any sequence of SQL statements. If this sequence does not contain an additional SUBTRANS BEGIN, all database modifications performed since the SUBTRANS BEGIN can be reversed using a SUBTRANS ROLLBACK.
The sequence, however, can also contain additional SUBTRANS BEGIN statements that open additional subtransactions. This means several nested subtransactions may be open at the same time.
If a subtransaction is concluded with SUBTRANS END, the performed modifications are kept. A subtransaction is closed, that is, the database system "forgets" the point in the transaction recorded with SUBTRANS BEGIN. An open subtransaction must exist for this purpose. If more than one subtransaction is open, the last opened subtransaction is closed, so that it is no longer considered open.
If a subtransaction is concluded with SUBTRANS ROLLBACK, all the database modifications made within a subtransaction are reversed and the subtransaction is closed. Any database modifications performed by any subtransactions within the subtransaction are reversed, irrespective of whether they were ended with SUBTRANS END or SUBTRANS ROLLBACK. All result tables generated within the subtransaction are closed.
An open subtransaction must exist for this purpose. If more than one subtransaction is open, the last opened subtransaction is reset. The subtransaction concerned is then no longer considered open.
The subtrans statement does not affect locks assigned to the transaction. In particular, SUBTRANS END and SUBTRANS ROLLBACK do not release any locks.
The subtrans statement is particularly useful in keeping the effects of subroutines or database procedures atomic, that is, it ensures that they either fulfill all their tasks or else have no effect. To this end, a SUBTRANS BEGIN is issued initially. If the subroutine succeeds in fulfilling its task, it is ended with a SUBTRANS END; in the event of an error, a SUBTRANS ROLLBACK is used to reverse all the modifications performed by the subroutine.
The COMMIT statement and the ROLLBACK statement close any open subtransactions implicitly.
See also:
SAVEPOINT/ROLLBACK TO Statement (savepoint/rollback_to_statement)