Home >> FAQs/Tutorials >> SQL Server FAQ

SQL Server FAQ - CONTINUE - Skipping Remaining Statements in a Loop Block

By: FYIcenter.com

(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; 
   PRINT CONVERT(VARCHAR(40),@date,107);
   SET @count = @count + 1;
   END
GO
Jan 02, 2000
Jan 09, 2000
Jan 16, 2000
Jan 23, 2000
Jan 30, 2000
Feb 06, 2000
Feb 13, 2000

  1. How To Use "IF ... ELSE IF ... ELSE ..." Statement Structures?
  2. How To Use "BEGIN ... END" Statement Structures?
  3. How To Use WHILE Loops?
  4. How To Stop a Loop Early with BREAK Statements?
  5. How To Skip Remaining Statements in a Loop Block Using CONTINUE Statements?

Related Articles:

More...


Other Tutorials/FAQs:

More...


Related Resources:

More...


Selected Jobs:

More...