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 ... ENDwith an earlyCONTINUEon the bad-case, leaving the main code at the loop's top level.
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.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
Notice 6 is missing. When @i = 6, CONTINUE jumped back to the top of the loop, skipping the PRINT.
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
Here's the bug to avoid — same logic as Example 1, but the increment is in the wrong place:
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
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.
| Statement | Where it goes | Loop continues? |
|---|---|---|
CONTINUE | Top of the loop — re-evaluates the WHILE condition | Yes — next iteration runs if condition still TRUE |
BREAK | First statement after the loop's END | No — loop is finished entirely |
CONTINUEskips 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.