//
you're reading...
Oracle 11g New Features, SQL & PL/SQL

Oracle 11g PLSQL Continue Statement


I found this new PLSQL enhancement in oracle 11g from the Oracle Database 11g New Features book that I think will be useful. This “continue” statement is used inside a looping structure to cause the next iteration of the loop to occur, instead of processing the remainder of the current loop iteration.

As always, an example gives better explanation (this example is taken from the book):

SET SERVEROUTPUT ON

DECLARE
   v_counter   NUMBER := 0;
BEGIN
   FOR COUNT IN 1 .. 10 LOOP
      v_counter := v_counter + 1;
      DBMS_OUTPUT.put_line ('v_Counter = ' || v_counter);
      CONTINUE WHEN v_counter > 5;
      v_counter := v_counter + 1;
   END LOOP;
END;
/

v_Counter = 1
v_Counter = 3
v_Counter = 5
v_Counter = 7
v_Counter = 8
v_Counter = 9
v_Counter = 10
v_Counter = 11
v_Counter = 12
v_Counter = 13

So you can see from the output than when the v_counter > 5, the process actually skip the line below the continue statement, making the counter to get incremented by 1.

You can also make the iteration to continue to outer loops by creating a label before the outer loop.

SET SERVEROUTPUT ON

DECLARE
v_counter   NUMBER := 0;
BEGIN
   <<BeforeLoop>>
   FOR outer_loop IN 1 .. 10 LOOP
       FOR COUNT IN 1 .. 10 LOOP
         v_counter := v_counter + 1;
         DBMS_OUTPUT.put_line ('v_Counter = ' || v_counter);
         CONTINUE BeforeLoop WHEN v_counter > 5;
         v_counter := v_counter + 1;
      END LOOP;
   END LOOP;
END;
/

About Yoel Susanto

Oracle Specialist

Discussion

No comments yet.

Leave a comment