Solved

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

Posted on 2016-10-28
7
85 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 28

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 77

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 13

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 13

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 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Via a live example, show how to take different types of Oracle backups using RMAN.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

820 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