How To Skip Remaining Statements in a Loop Block Using CONTINUE Statements?

Submitted by: Administrator
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

Submitted by: Administrator

Read Online MS SQL Server Job Interview Questions And Answers