The 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 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.
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. An open subtransaction must exist for this purpose.
SUBTRANS ROLLBACK reverses all database modifications performed within a subtransaction and then closes the subtransaction. 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 open subtransaction exists, the last opened subtransaction is rolled back. 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 fulfil 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.