Live ORACLE

if you are Oracle Developer ,than this Blog most likely will make you happy.

About my Blog

This Blog particularized for Oracle Developers ... you will see interesting Codes for SQL , PL/SQL as well as new ideas for Developer Suite and Client Tools that will help you in your professional life ... and I hope I reach for your satisfaction.

About Me

I'm Hany Freedom , 25 years old , I live in EL-Minia City in Egypt , I'm Moderator at ArabOUG.org the best Arabic Forum for ORACLE on the Net. if you interested to know more about me .... just Click Here.

Retrying a Transaction After an Exception

DROP TABLE results CASCADE CONSTRAINTS PURGE;


CREATE
TABLE results (
res_name VARCHAR(20),
res_answer VARCHAR2(3));



DROP
INDEX res_name_ix;



CREATE
UNIQUE INDEX res_name_ix ON results (
res_name);



INSERT
INTO results
VALUES ('SMYTHE',
'YES');



INSERT
INTO results
VALUES ('JONES',
'NO');



DECLARE

NAME VARCHAR2(20) := 'SMYTHE';
answer VARCHAR2(3) := 'NO';
suffix NUMBER := 1;
BEGIN
FOR i IN 1.. 5 LOOP -- try 5 times
BEGIN -- sub-block begins
SAVEPOINT start_transaction; -- mark a savepoint
/* Remove rows from a table of survey results. */
DELETE FROM results
WHERE res_answer = 'NO';
/* Add a survey respondent's name and answers. */
INSERT INTO results
VALUES (NAME,
answer);
-- raises DUP_VAL_ON_INDEX if two respondents have the same name
COMMIT;
EXIT;
EXCEPTION
WHEN dup_val_on_index THEN
ROLLBACK TO start_transaction; -- undo changes
suffix := suffix
+ 1; -- try to fix problem
NAME := NAME
||To_char(suffix);
END; -- sub-block ends
END LOOP;
END;
/


Share/Save/Bookmark

0 comments:

Post a Comment



Newer Posts Older Posts Home Page
 
http://www.dpriver.com/images/sqlpp-banner-2.png

Thanks for reading my Blog ... you Visitor Number :-