Using savepoint, PL-SQL Programming

Using SAVEPOINT

The SAVEPOINT names and marks the present point in the processing of a transaction. Used with the ROLLBACK TO statement, the savepoints undo parts of a transaction rather than the entire transaction. In the illustration below, you mark a savepoint before doing an insert. When the INSERT statement tries to store a duplicate value in the empno column, the predefined exception DUP_VAL_ON_INDEX is raised. In that situation, you roll back to the savepoint, undoing merely the insert.

DECLARE

emp_id emp.empno%TYPE;

BEGIN

UPDATE emp SET ... WHERE empno = emp_id;

DELETE FROM emp WHERE ...

...

SAVEPOINT do_insert;

INSERT INTO emp VALUES (emp_id, ...);

EXCEPTION

WHEN DUP_VAL_ON_INDEX THEN

ROLLBACK TO do_insert;

END;

If you roll back to a savepoint, any savepoints marked after the savepoint are erasing. Though, the savepoint to which you roll back is not erasing. For illustration, if you mark five savepoints, and then roll back to the third, only the fourth and the fifth are erase. A simple rollback or commit erases all savepoints.

When you mark a savepoint within a recursive subprogram, the new instances of the

SAVEPOINT statements are executed at each level in the recursive fall. Though, you can only roll back to the nearly all recently marked savepoint.

The Savepoint names are undeclared identifiers and can be reused within the transaction. This moves the savepoint from its old position to the persent point in the transaction. And hence, the rollback to the savepoint affects only the present part of your transaction. An illustration is as shown:

BEGIN

...

SAVEPOINT my_point;

UPDATE emp SET ... WHERE empno = emp_id;

...

SAVEPOINT my_point; -- move my_point to current point

INSERT INTO emp VALUES (emp_id, ...);

EXCEPTION

WHEN OTHERS THEN

ROLLBACK TO my_point;

END;

The number of active savepoints per session is limitless. An active savepoint is the one marked as the last commit or rollback.

Posted Date: 10/4/2012 5:15:24 AM | Location : United States







Related Discussions:- Using savepoint, Assignment Help, Ask Question on Using savepoint, Get Answer, Expert's Help, Using savepoint Discussions

Write discussion on Using savepoint
Your posts are moderated
Related Questions
Project Description: I want to write some SQL statements. The things I need are between pages 5-7. The only problem is that i want it till tomorrow. Skills required is SQL

Remote Operations: As the illustration shows below, the PL/SQL subprograms can execute the dynamic SQL statements which refer to the objects on a remote database: PROCEDURE

Defining REF CURSOR Types To make cursor variables, you take 2 steps. At first, you define a REF CURSOR type, and then declare the cursor variables of that type. You can defin

Using PRIOR and NEXT The PRIOR(n) returns the index number that precede index n in a collection. The NEXT(n) returns the index number which succeed the index n. If n has no pr

Example of Using Aggregation on Nested Tables Example: How many students sat each exam WITH C_ER AS (SELECT CourseId, CAST (TABLE (SELECT DISTINCT StudentId, Mark FROM EXAM

Parameter and Keyword Description: SQL: This SQL is the name of the implicit SQL cursor. %FOUND: This attribute results TRUE if an INSERT, DELETE, or UPDATE state

INSERT Command in SQL Loosely speaking, INSERT takes the rows of a given source table and adds them to the specified target table, retaining all the existing rows in the targ

Committing and Rolling Back The COMMIT and ROLLBACK end the active autonomous transaction but do not exit the autonomous routine. As the figure shows, if one transaction ends,

Effects of NULL Operator As a general rule-but not a universal one-if NULL is an argument to an invocation of a system-defined read-only operator, then NULL is the result of t

Effects of NULL The numeric variable X, perhaps of type INTEGER, might be assigned NULL. In that case the result of evaluating X + 1 is NULL, and so SET Y = X + 1 assigns NULL