Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Is it possible to CONTINUE a loop from an exception in oracle pl/sql?

Posted on 2016-10-28
7
Medium Priority
?
166 Views
Last Modified: 2016-11-01
Is it possible to CONTINUE a loop from an exception in oracle pl/sql?
 
Here is part of the code, When I run the program it's not recognizing the CONTINUE statement at the exception.  I want the program to continue after the insert statement at the exception.

      Begin
       
          dbms_output.put_line('inside_pprec');
 
           SELECT 'Z'
           INTO p_id
           FROM LEVY
           WHERE parid = dq_rec.parid
           AND taxyr = dq_rec.taxyr
           AND (dq_rec.user9 is null or dq_rec.user10 is null)
           AND (CASESTAT <> 'A' or cur = 'D');
           dbms_output.put_line (''||P_id );
           
        IF P_id = 'Z'
          THEN
               UPDATE LEVY
                SET DEACTIVAT = null,
                STATUS            = '3',
                LEVYTYPE         = 'SC',
                CASESTAT       = 'A',
                SALEDATE       = '&&p_Saledt',
                LEVY1         = '1',
                LEVYDATE1         = SYSDATE,
                USER1          = (select nvl(decode(dq_rec.prop_type,'H',r_ppadmn.user30,r_ppadmn.user5),user11) from PARDAT where parid = dq_rec.parid  AND taxyr = dq_rec.taxyr and cur = 'Y'),
                USER2          = decode(dq_rec.prop_type,'MH',r_ppadmn.user2),
                USER3          = decode(dq_rec.rolltype,'RE',null,r_ppadmn.user3),
                USER4         = (select nvl(flag1,'N') from ASMT where parid = dq_rec.parid  AND taxyr = dq_rec.taxyr and cur = 'Y'),
                USER20         = decode(dq_rec.rolltype,'RE',null,r_ppadmn.user19),
                USER21         = decode(dq_rec.rolltype,'RE',null,r_ppadmn.user17),    
                USER23         = decode(dq_rec.rolltype,'RE',null,r_ppadmn.user18),  
                USER24         = decode(dq_rec.rolltype,'RE',null,r_ppadmn.user51)    
                WHERE parid = dq_rec.parid
                AND taxyr = dq_rec.taxyr;
         END IF;
           
       Exception
        When no_data_found
        THEN
                   dbms_output.put_line ('Exception'||p_caseno|| '  '||dq_rec.PARID);
            INSERT INTO LEVY (JUR,PARID,TAXYR,ROLLTYPE,CASENO,STATUS,LEVYTYPE,CASESTAT,LEVY1,LEVYDATE1,USER1,USER2,USER3,USER20,USER21,USER23,USER24,user4)
            VALUES(dq_rec.JUR,dq_rec.PARID,dq_rec.TAXYR,dq_rec.ROLLTYPE,p_caseno,'3','SC','A','1',SYSDATE,decode(dq_rec.prop_type,'H',r_ppadmn.user30,r_ppadmn.user5),
                    decode(dq_rec.prop_type,'MH',r_ppadmn.user2),decode(dq_rec.rolltype,'RE',null,r_ppadmn.user3),decode(dq_rec.rolltype,'RE',null,r_ppadmn.user19),decode(dq_rec.rolltype,'RE',null,r_ppadmn.user17),decode(dq_rec.rolltype,'RE',null,r_ppadmn.user18),decode(dq_rec.rolltype,'RE',null,r_ppadmn.user51),(select nvl(flag1,'N') from ASMT where parid = dq_rec.parid  AND taxyr = dq_rec.taxyr and cur = 'Y') );
             CONTINUE;      
 --      commit;
       End;
0
Comment
Question by:Tanuja Kadam
7 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 41864643
there are no loops in your code.  What are you expecting to "continue"  ?
0
 
LVL 35

Expert Comment

by:Pawan Kumar
ID: 41864723
Can you try the code after removing the Continue keyword.  Continue statement is for loops. Sample code..

DECLARE
   Counter number(2) := 1;
BEGIN
    
   WHILE Counter < 5 LOOP
      dbms_output.put_line (Counter);
      Counter := Counter + 1;
      IF Counter = 3 THEN         
         Counter := Counter + 1;
         CONTINUE;
      END IF;
   END LOOP;
END;
/

Open in new window


O/p
1
2
4

So effectively continue statement will move the execution to the next iteration.
Hope it helps !!
0
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 41864734
A pretty big guess here is that is the nested PL/SQL block of a larger stored procedure.

If so, I don't think the continue is where it should be.  The nested block is sort of it's own island and controls itself.

If it gets a no_data_found exception then it does the insert.

HOWEVER, if the insert causes an exception, there is no handler for that so it isn't trapped.

You might need to put another nested pl/sql block in the insert and trap errors inside there.
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 14

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 41866539
The exact answer to the titled question
Is it possible to CONTINUE a loop from an exception in oracle pl/sql?
is quite as simple as short: NO
0
 
LVL 14

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 41866541
If a loop breaks due to an exception where do you want to continue?! The whole context is lost. If you handle that very exception within the loop's boundaries then the answer would be sort of yes (but not in the context of the main question).
0
 

Author Comment

by:Tanuja Kadam
ID: 41866745
Yes...I wouls like the code to continue after the execution of exception(insert). There is more code after this block.
0
 
LVL 78

Accepted Solution

by:
slightwv (䄆 Netminder) earned 2000 total points
ID: 41866772
Then create the nested PL/SQL block with its own exception handler as I suggested.

This is untested but something like this:
...
Exception
         When no_data_found
         THEN
                    dbms_output.put_line ('Exception'||p_caseno|| '  '||dq_rec.PARID);
            BEGIN
                 INSERT INTO LEVY      (JUR,PARID,TAXYR,ROLLTYPE,CASENO,STATUS,LEVYTYPE,CASESTAT,LEVY1,LEVYDATE1,USER1,USER2,USER3,USER20,USER21,USER23,USER24,user4)
                 VALUES(dq_rec.JUR,dq_rec.PARID,dq_rec.TAXYR,dq_rec.ROLLTYPE,p_caseno,'3','SC','A','1',SYSDATE,decode(dq_rec.prop_type,'H',r_ppadmn.user30,r_ppadmn.user5),
                        decode(dq_rec.prop_type,'MH',r_ppadmn.user2),decode(dq_rec.rolltype,'RE',null,r_ppadmn.user3),decode(dq_rec.rolltype,'RE',null,r_ppadmn.user19),decode(dq_rec.rolltype,'RE',null,r_ppadmn.user17),decode(dq_rec.rolltype,'RE',null,r_ppadmn.user18),decode(dq_rec.rolltype,'RE',null,r_ppadmn.user51),(select nvl(flag1,'N') from ASMT where parid = dq_rec.parid  AND taxyr = dq_rec.taxyr and cur = 'Y') );
              
             EXCEPTION WHEN OTHERS THEN NULL;
            END;
        End; 
...

Open in new window


If you want to take some other action or only continue on specific errors, adjust the WHEN OTHERS as necessary.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question