Skip to Main Content
  • Questions
  • How to ignore failed insert /update stmts in a cursor and commit outside cursor loop

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Manoj.

Asked: February 04, 2009 - 10:56 pm UTC

Last updated: February 06, 2009 - 2:58 pm UTC

Version: 9.2.0

Viewed 1000+ times

You Asked

My program is a critical reporting called thro ksh script.
I want to ensure that in no case all insert data gets rolled backed due to any data issue or ora errors.

1.How to ensure that inserts which have failed in exception block are ignored
and successful inserts will commit outside cursor loop.
2.Do you need to handle exception when others for each insert stmts
in the cursor loop.
3.If cursor data vaolume is high do I need commit after nth record at a time.
Normally our project programs log errors to a os file using utl_file thro a separate stored procedure.

4.Assuming I commit after every 5000/nth record in cursor loop.
Can pl/sql for this sample example ensure that all dmls will be part of a single transaction .

Note:I've lot of select stmts and select on max column values before I insert into table in cursor loop.

5.What is the best practice we can employ in pl/sql
when inserted table data is being used for reporting purpose (BO reports )
and being monitored closely


1.declare a cursor on tables (A,B,C)
2.open cursor
3.fetch cursor in a loop
begin
insert into table D
exception
when others
log into exception table
end;
if rec cnt = 5000
commit;
end if;
end loop;
close cursor;

update control table
where clause

commit;

and Tom said...

1) for 9i, see

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:8784259916366

for 10g and up, see
http://asktom.oracle.com/Misc/how-cool-is-this.html

and do not commit in the for loop, excessively bad practice as you have psuedo coded it. Unless you are going to write A LOT more code, you have to remove it.

Reasoning: very simple, when your code does fail, you need to restart it, I see no logic here to restart your process without attempting to recopy the data you already copied

2) you need to handle it at the STATEMENT LEVEL, forget about the cursor for loop or any other control structures. You want the insert to "succeed" even when it fails, therefore you need an exception block around the insert, regardless of what it is "in"

3) no.

4) no, not if you commit, if you commit - you are saying "make it persistent". It will not be part of a single transaction.

5) that does not compute. There are no "goals" here, no context.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Manoj Gurnani, February 07, 2009 - 12:09 am UTC


More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library