SQL Keywords

SQL CONTINUE

What is the SQL CONTINUE statement?

CONTINUE immediately skips the remaining statements in the current loop body and starts the next iteration.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL CONTINUE: PostgreSQL (PL/pgSQL), SQL Server (T-SQL), Oracle 11g+ (PL/SQL). MySQL uses ITERATE instead of CONTINUE.

SQL CONTINUE Full Explanation

CONTINUE is a loop-control statement available in procedural SQL dialects such as PL/pgSQL, T-SQL, and Oracle PL/SQL. When the statement executes, any subsequent commands in the current iteration are bypassed and control jumps to the loop’s condition check (or directly to the top, depending on loop type). This lets you ignore certain rows or situations without breaking out of the loop entirely. Optionally, you can supply a label to identify which surrounding loop to continue when loops are nested. CONTINUE can only appear inside looping constructs (LOOP, WHILE, FOR, FOREACH). Attempting to use it outside a loop or inside a function that does not support procedural flow raises an error.

SQL CONTINUE Syntax

CONTINUE;
-- or, with a labeled loop
CONTINUE label_name;

SQL CONTINUE Parameters

  • label_name (identifier) - Optional. Name of an enclosing loop to target when loops are nested. If omitted, the innermost loop is used.

Example Queries Using SQL CONTINUE

-- Example 1: Skip inactive users during batch update
FOR r IN SELECT id, active FROM users LOOP
    IF r.active = false THEN
        CONTINUE; -- go to next user
    END IF;
    UPDATE users SET last_seen = NOW() WHERE id = r.id;
END LOOP;

-- Example 2: Using a label in nested loops
outer_loop:
FOR y IN 1..12 LOOP
    inner_loop:
    FOR d IN 1..31 LOOP
        IF d > 28 THEN
            CONTINUE outer_loop; -- jump to next month
        END IF;
        INSERT INTO calendar(month,day) VALUES (y,d);
    END LOOP;
END LOOP outer_loop;

Expected Output Using SQL CONTINUE

  • Statements after CONTINUE in the same loop iteration are not executed; control returns to the loop’s next cycle
  • Updates, inserts, or variable changes placed before CONTINUE still persist

Use Cases with SQL CONTINUE

  • Filtering out rows that do not meet criteria without ending the loop
  • Skipping over error-prone records and processing them later
  • Jumping from deeply nested loops directly to an outer loop iteration via labeled CONTINUE

Common Mistakes with SQL CONTINUE

  • Using CONTINUE outside a loop, which raises a syntax error
  • Confusing CONTINUE with EXIT; EXIT terminates the loop entirely, not just the current iteration
  • Forgetting to specify a label when intending to target an outer loop, leading to unexpected behavior

Related Topics

EXIT, BREAK, LOOP, WHILE, FOR, ITERATE, RETURN

First Introduced In

PostgreSQL 8.4 (PL/pgSQL)

Frequently Asked Questions

What is the difference between CONTINUE and EXIT?

CONTINUE moves immediately to the next loop iteration, while EXIT terminates the loop and proceeds with the first statement after the loop.

Can I use CONTINUE without a loop label?

Yes. If no label is supplied, CONTINUE affects the innermost loop that encloses it.

Does CONTINUE roll back the work done earlier in the iteration?

No. Any changes executed before CONTINUE remain committed to the transaction unless you explicitly roll them back.

What happens if I place CONTINUE outside a loop?

The database raises a syntax error because CONTINUE is only valid inside looping constructs.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!