Home >> FAQs/Tutorials >> SQL Server FAQ
SQL Server FAQ - CONTINUE - Skipping Remaining Statements in a Loop Block
(Continued from previous topic...)
How To Skip Remaining Statements in a Loop Block Using CONTINUE Statements?
If you want to skip the remaining statements in a loop block,
you can use the CONTINUE statement.
The tutorial exercise below shows you how to use a CONTINUE statement to skip
the remaining statements and continue the next iteration:
-- Printing first 7 Sundays in 2000
DECLARE @date DATETIME;
DECLARE @count INT;
SET @date = '1999-12-31';
SET @count = 0;
WHILE DATEPART(YEAR, @date) < 2001 BEGIN
SET @date = DATEADD(DAY, 1, @date);
IF @count = 7 BREAK;
IF DATENAME(WEEKDAY, @date) <> 'Sunday' CONTINUE;
SET @count = @count + 1;
Jan 02, 2000
Jan 09, 2000
Jan 16, 2000
Jan 23, 2000
Jan 30, 2000
Feb 06, 2000
Feb 13, 2000
- How To Use "IF ... ELSE IF ... ELSE ..." Statement Structures?
- How To Use "BEGIN ... END" Statement Structures?
- How To Use WHILE Loops?
- How To Stop a Loop Early with BREAK Statements?
- How To Skip Remaining Statements in a Loop Block Using CONTINUE Statements?