Best Practices for Using CONTINUE in Oracle PL/SQL


Intro

In this article, I will raise awareness on a PLSQL feature that is too often ignored.

The CONTINUE clause in PL/SQL is a powerful control structure that allows you to skip the remaining statements in the current iteration of a loop and move directly to the next iteration.

It helps make code cleaner and avoids deeply nested IF conditions when certain cases should be ignored.

As far as I remember, this feature was introduced in Oracle 11g and it can be commonly used in LOOP, WHILE, and FOR loops.

What is the CONTINUE Clause?

In Oracle Database’s procedural language PL/SQL, the CONTINUE statement:

  • Immediately proceeds to the next iteration
  • Skips the rest of the loop body
  • Can be used conditionally (CONTINUE WHEN); example to follow

Basic Syntax

Example 1: Simple CONTINUE in a LOOP

CONTINUE;

IN is example we would want to exclude from processing the rows with value 3:

set serveroutput on
BEGIN
FOR i IN 1..12 LOOP
IF i = 3 THEN
CONTINUE;
END IF;
DBMS_OUTPUT.PUT_LINE('Value: ' || i);
END LOOP;
END;

returns as below and when i = 3, the CONTINUE skips printing and moves to the next iteration

Value: 1
Value: 2
Value: 4
Value: 5
Value: 6
Value: 7
Value: 8
Value: 9
Value: 10
Value: 11
Value: 12
PL/SQL procedure successfully completed

Example 2: Using CONTINUE WHEN

This is a cleaner and more concise way.Code snippet example:

BEGIN
FOR i IN 1..8 LOOP
CONTINUE when i >6;
DBMS_OUTPUT.PUT_LINE('Value: ' || i);
END LOOP;
END;

Only the values smaller or equal to six should be printed:

Value: 1
Value: 2
Value: 3
Value: 4
Value: 5
Value: 6
PL/SQL procedure successfully completed.

Example 3: Skipping Even Numbers

The example will skip the even numbers from processing and list all odd numbers up to 30:

BEGIN
FOR i IN 1..30 LOOP
CONTINUE WHEN MOD(i, 2) = 0;
DBMS_OUTPUT.PUT_LINE('Odd number==> ' || i);
END LOOP;
END;
Odd number==> 1
Odd number==> 3
Odd number==> 5
Odd number==> 7
Odd number==> 9
Odd number==> 11
Odd number==> 13
Odd number==> 15
Odd number==> 17
Odd number==> 19
Odd number==> 21
Odd number==> 23
Odd number==> 25
Odd number==> 27
Odd number==> 29
PL/SQL procedure successfully completed.

Example 4: CONTINUE in WHILE Loop

Will stop processing when the values goes above 8:

DECLARE
i NUMBER := 0;
BEGIN
WHILE i < 15 LOOP
i := i + 1;
CONTINUE WHEN i > 8;
DBMS_OUTPUT.PUT_LINE('Value: ' || i);
END LOOP;
END;
Output:
Value: 1
Value: 2
Value: 3
Value: 4
Value: 5
Value: 6
Value: 7
Value: 8
PL/SQL procedure successfully completed.

Example 5: Nested Loops with CONTINUE

As expected, CONTINUE statement will ‘continue’ to work within the inner loop :)

For this case, the rows where j has value 2 will be skipped from displaying (or processing):

BEGIN
FOR i IN 1..3 LOOP
FOR j IN 1..3 LOOP
CONTINUE WHEN j = 2;
DBMS_OUTPUT.PUT_LINE('i=' || i || ', j=' || j);
END LOOP;
END LOOP;
END;

generates:

i=1, j=1
i=1, j=3
i=2, j=1
i=2, j=3
i=3, j=1
i=3, j=3
PL/SQL procedure successfully completed.

Best Practices

Disclaimer: like with the other best practices I publish, please note that those are derived from real production examples. in case you need help, please reach out or otherwise, use at own risk!

Prefer CONTINUE WHEN for readability
Avoid overusing nested IF blocks—use CONTINUE instead
Use labels carefully to avoid confusing logic in nested loops
Combine with conditions for cleaner filtering logic

Summary

The CONTINUE clause in PL/SQL is a simple yet powerful feature that improves:

  • Code clarity
  • Maintainability
  • Performance (by skipping unnecessary logic)

It’s especially useful in loops where certain conditions should bypass processing without terminating the loop entirely

Also, it is great for:

  • Data validation
  • ETL jobs
  • Bulk processing
  • Error handling

Considering how important those use cases are for the modern Entreprise, I would like to see this feature used in more such cases, if not all!


Discover more from Radu Pârvu

Subscribe to get the latest posts sent to your email.

, , , , , , ,

Leave a Reply

Discover more from Radu Pârvu

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Radu Pârvu

Subscribe now to keep reading and get access to the full archive.

Continue reading