CONTINUE

CONTINUE is a Transact-SQL statement that is used within WHILE loops. Its function is to immediately jump to the start of the loop, allowing the execution sequence to bypass the rest of the loop's code block and begin the next iteration.

Example

DECLARE
i number := 1;
BEGIN
WHILE i <= 10 LOOP
IF i = 5 THEN
i := i + 1;
CONTINUE;
END IF;
dbms_output.put_line(i);
i := i+1;
END LOOP;
END;

Output

1
2
3
4
6
7
8
9
10

Explanation

In the above example, a loop runs ten times, printing numbers from 1 to 10. However, when the number is 5, the CONTINUE statement is encountered. This causes the control to go back to the start of the loop, skipping the print statement for the number 5. Thus, the output doesn’t include 5.

Example

DECLARE @Num INT;
SET @Num = 1;
WHILE @Num <= 10
BEGIN
IF @Num = 5
CONTINUE;
PRINT @Num;
SET @Num = @Num + 1;
END

Output

1
2
3
4
6
7
8
9
10

Explanation

The CONTINUE keyword in SQL Server is used to start the next iteration of a loop, skipping any subsequent statements in the current iteration. In the provided example, a WHILE loop is used to iterate over integer values from 1 to 10. When the integer value is equal to 5, the CONTINUE keyword skips the print statement for the current iteration, resulting in the value 5 being omitted from the output.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.