Oracle PL/SQL CONTINUE T-SQL · Control-of-Flow · CONTINUE

T-SQL CONTINUE Statement

Master T-SQL CONTINUE — skip the rest of the current loop iteration. In-loop filtering, the critical 'increment before CONTINUE' rule, the infinite-loop trap to avoid, and comparison with BREAK.

CONTINUE skips the rest of the current iteration of the innermost WHILE loop and jumps back to the top — where the loop condition is re-evaluated. The loop keeps going; only the current pass is cut short.

Use it when, partway through processing an item, you decide "this one isn't relevant — go to the next" without bailing out of the whole loop.

Common Uses

  • Filtering inside a loop. Skip rows, items, or values that don't meet your criteria.
  • Avoiding deeper nesting. Replace IF (good) BEGIN ... lots of code ... END with an early CONTINUE on the bad-case, leaving the main code at the loop's top level.
WHILE condition BEGIN -- some statements IF skip_this_one BEGIN CONTINUE; END -- skipped if CONTINUE fired END
⚠️ Update your counter BEFORE CONTINUE. If SET @i = @i + 1; sits after CONTINUE in your loop body, it never runs when CONTINUE fires — and the WHILE condition stays true forever. The result is an infinite loop. Always advance the counter before the CONTINUE.
Example 1 — Skipping One Value
T-SQL — Print 1 to 10, but skip 6
DECLARE @i INT = 0;

WHILE @i < 10
BEGIN
    SET @i = @i + 1;            -- IMPORTANT: increment first

    IF @i = 6
        CONTINUE;               -- skip the PRINT below for 6

    PRINT 'i value: ' + CAST(@i AS VARCHAR(10));
END
OUTPUT
Messages
Commands completed successfully.
i value: 1
i value: 2
i value: 3
i value: 4
i value: 5
i value: 7
i value: 8
i value: 9
i value: 10

Notice 6 is missing. When @i = 6, CONTINUE jumped back to the top of the loop, skipping the PRINT.

Example 2 — Filter Even Numbers Only
T-SQL — Print only odd numbers
DECLARE @i INT = 0;

WHILE @i < 10
BEGIN
    SET @i = @i + 1;

    -- Skip even numbers
    IF @i % 2 = 0
        CONTINUE;

    PRINT 'Odd: ' + CAST(@i AS VARCHAR(10));
END
OUTPUT
Messages
Commands completed successfully.
Odd: 1
Odd: 3
Odd: 5
Odd: 7
Odd: 9
Example 3 — The Infinite-Loop Trap

Here's the bug to avoid — same logic as Example 1, but the increment is in the wrong place:

T-SQL — DON'T DO THIS — INFINITE LOOP
DECLARE @i INT = 0;

WHILE @i < 10
BEGIN
    IF @i = 6
        CONTINUE;               -- ⚠️ jumps back to WHILE — @i is still 6 forever

    PRINT 'i value: ' + CAST(@i AS VARCHAR(10));
    SET @i = @i + 1;            -- ⚠️ never reached when @i = 6
END
OUTPUT
Messages
Query cancelled by user — infinite loop
i value: 0
i value: 1
i value: 2
i value: 3
i value: 4
i value: 5
(loop spins forever at @i = 6 — must be cancelled)

Once @i becomes 6, CONTINUE skips the increment and jumps back to the WHILE condition. @i is still 6, so the IF fires again, CONTINUE again, forever. The fix: move SET @i = @i + 1 above the CONTINUE, as shown in Example 1.

StatementWhere it goesLoop continues?
CONTINUETop of the loop — re-evaluates the WHILE conditionYes — next iteration runs if condition still TRUE
BREAKFirst statement after the loop's ENDNo — loop is finished entirely
  • CONTINUE skips the rest of the current iteration and re-tests the WHILE condition.
  • Affects only the innermost loop, just like BREAK.
  • Always advance the loop counter before CONTINUE — otherwise you've built an infinite loop.
  • Useful for in-loop filtering ("skip even numbers", "skip rows with NULL date") — keeps the main path of the loop body un-indented.