CONTINUE command in SQL Server

The CONTINUE command is used to give command execution authority to the last expression of the loop. That means upside-down to the top of the loop, all the commands that follow in the loop containing CONTINUE will be ignored without execution.

The CONTINUE command is used to give command execution authority to the last expression of the loop. That means upside-down to the top of the loop, all the commands that follow in the loop containing CONTINUE will be ignored without execution. This article will detail how to use this command in SQL Server.

Syntax

To use the CONTINUE statement in SQL Server, we use the following syntax:

 TIẾP TỤC; 

Note:

  1. There are no parameters and arguments in the CONTINUE statement.
  2. You use the CONTINUE statement to return to the WHILE loop and execute its subsequent conditions.

For example

 DECLARE @Number INT = 1; 
DECLARE @Total INT = 0;

@Number WHILE <= 10
BEGIN
IF @NUMBER = 5
BREAK;

ELSE
SET @Total = @Total + @Number;
SET @Number = @Number + 1;
TIẾP TỤC;
END;

END;

PRINT @Total;
GO

In this example using the CONTINUE statement, we will restart the WHILE loop if the variable @NUMBER is different from 5 as specified by the IF . ELSE statement.

Previous article: BREAK (Control Interrupt) command in SQL Server

Next lesson: GOTO command in SQL Server

4 ★ | 1 Vote