8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | 21c | 23c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » 11g » Here

CONTINUE Statement in Oracle Database 11g Release 1

The CONTINUE statement has finally been added to PL/SQL in Oracle 11g, allowing you to jump out of the current iteration of a loop.

Related articles.

CONTINUE and CONTINUE WHEN

The CONTINUE statement jumps out of the current loop iteration and starts the next one. It can be used on its own, or as part of a CONTINUE WHEN statement, as shown below.

SET SERVEROUTPUT ON
DECLARE
  l_number    NUMBER := 0;
BEGIN
  FOR i IN 1 .. 100 LOOP
    CONTINUE WHEN MOD(i,2) = 0;

    -- Do something here!
    l_number := l_number + 1;
  END LOOP;

  DBMS_OUTPUT.put_line('CONTINUE WHEN : ' || l_number);

  l_number := 0;

  FOR i IN 1 .. 100 LOOP
    IF MOD(i,2) = 0 THEN
      CONTINUE;
    END IF;

    -- Do something here!
    l_number := l_number + 1;
  END LOOP;

  DBMS_OUTPUT.put_line('IF .. CONTINUE: ' || l_number);
END;
/
CONTINUE WHEN : 50
IF .. CONTINUE: 50

PL/SQL procedure successfully completed.

SQL>

Pre-11g

This type of processing has always been possible using IF statements either on their own or with exceptions or GOTO statements, but the CONTINUE statement is neater and brings PL/SQL in line with other langauges. The following examples show the type of code necessary to perform the same task before the CONTINUE statement was added to PL/SQL.

SET SERVEROUTPUT ON
DECLARE
  ex_continue EXCEPTION;
  l_number    NUMBER := 0;
BEGIN
  FOR i IN 1 .. 100 LOOP
    BEGIN
      IF MOD(i,2) != 0 THEN
        RAISE ex_continue;
      END IF;

      -- Do something here!
      l_number := l_number + 1;
    EXCEPTION
      WHEN ex_continue THEN
        NULL;
    END;
  END LOOP;

  DBMS_OUTPUT.put_line('EXCEPTION: ' || l_number);

  l_number := 0;

  FOR i IN 1 .. 100 LOOP
    IF MOD(i,2) != 0 THEN

      -- Do something here!
      l_number := l_number + 1;

    END IF;
  END LOOP;

  DBMS_OUTPUT.put_line('IF       : ' || l_number);

  l_number := 0;

  FOR i IN 1 .. 100 LOOP
    IF MOD(i,2) = 0 THEN
      GOTO label_continue;
    END IF;

    -- Do something here!
    l_number := l_number + 1;

    << label_continue >>
    NULL;
  END LOOP;

  DBMS_OUTPUT.put_line('GOTO     : ' || l_number);
END;
/
EXCEPTION: 50
IF       : 50
GOTO     : 50

PL/SQL procedure successfully completed.

SQL>

For more information see:

Hope this helps. Regards Tim...

Back to the Top.