Development SQL Server Stored Procedures

Continue

The CONTINUE statement stops the current iteration of the loop and starts the new one. The following illustrates the syntax of the CONTINUE statement:

1234567WHILE Boolean_expressionBEGIN    — code to be executed    IF condition        CONTINUE;    — code will be skipped if the condition is metEND

In this syntax, the current iteration of the loop is stopped once the condition evaluates to TRUE. The next iteration of the loop will continue until the Boolean_expression evaluates to FALSE.

Similar to the BREAK statement, the CONTINUE statement is often used in conjunction with an IF...ELSE statement. Note that this is not mandatory though.

SQL Server CONTINUE example

The following example illustrates how the CONTINUE statement works.

123456789DECLARE @counter INT = 0; WHILE @counter < 5BEGIN    SET @counter = @counter + 1;    IF @counter = 3        CONTINUE;     PRINT @counter;END

Here is the output:

12341245

In this example:

First, we declared a variable named @counter and set its value to zero.

Then, the WHILE loop started. Inside the WHILE loop, we increased the counter by one in each iteration. If the @counter was three, we skipped printing out the value using the CONTINUE statement. That’s why in the output, you do not see the number three is showing up.

1 thought on “Continue”

Leave a Reply

Your email address will not be published. Required fields are marked *