Solved

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

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

Expert Comment

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

Expert Comment

by:Pawan Kumar Khowal
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 76

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 76

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.

Join & Write a Comment

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now