How To Skip Remaining Statements in a Loop Block Using CONTINUE Statements?
Submitted by: AdministratorIf 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
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
Top MS SQL Server Questions
☺ | How To Execute the Cursor Queries with "OPEN" Statements? |
☺ | What Happens If Strings Are Casted into Wrong Code Pages in MS SQL Server? |
☺ | How To Create a View on an Existing Table in MS SQL Server? |
☺ | PHP MSSQL - How To Display a Past Time in Days, Hours and Minutes? |
☺ | How To Create a Large Table with Random Data for Index Testing in MS SQL Server? |
Top Databases Programming Categories
☺ | RDBMS Interview Questions. |
☺ | SQL Interview Questions. |
☺ | SSRS Interview Questions. |
☺ | Database Administrator (DBA) Interview Questions. |
☺ | Sybase Interview Questions. |