10.1.4. SAVEPOINT

Used for

Creating a savepoint

Available

DSQL

Syntax

  1. SAVEPOINT sp_name
Table 174. SAVEPOINT Statement Parameter
ParameterDescription

sp_name

Savepoint name. Available only in DSQL

The SAVEPOINT statement creates an SQL:99-compliant savepoint that acts as a marker in the “stack” of data activities within a transaction. Subsequently, the tasks performed in the “stack” can be undone back to this savepoint, leaving the earlier work and older savepoints untouched. Savepoint mechanisms are sometimes characterised as “nested transactions”.

If a savepoint already exists with the same name as the name supplied for the new one, the existing savepoint is deleted and a new one is created using the supplied name.

To roll changes back to the savepoint, the statement ROLLBACK TO SAVEPOINT is used.

Memory Considerations

The internal mechanism beneath savepoints can consume large amounts of memory, especially if the same rows receive multiple updates in one transaction. When a savepoint is no longer needed, but the transaction still has work to do, a RELEASE SAVEPOINT statement will erase it and thus free the resources.

Sample DSQL session with savepoints

  1. CREATE TABLE TEST (ID INTEGER);
  2. COMMIT;
  3. INSERT INTO TEST VALUES (1);
  4. COMMIT;
  5. INSERT INTO TEST VALUES (2);
  6. SAVEPOINT Y;
  7. DELETE FROM TEST;
  8. SELECT * FROM TEST; -- returns no rows
  9. ROLLBACK TO Y;
  10. SELECT * FROM TEST; -- returns two rows
  11. ROLLBACK;
  12. SELECT * FROM TEST; -- returns one row

See also

ROLLBACK TO SAVEPOINT, RELEASE SAVEPOINT