In this blog post, let’s learn about the error message “136 – Cannot use a CONTINUE statement outside the scope of a WHILE statement.” in Microsoft SQL Server, the reason why it appears and the solution to fix it.
SQL Server Error Message
136 – Cannot use a CONTINUE statement outside the scope of a WHILE statement.
Reason for the Error
SQL Server Error Msg 136 occurs when a CONTINUE statement is used outside the scope of a WHILE loop. This error message indicates that the CONTINUE statement can only be used within the body of a WHILE loop to skip to the next iteration of the loop.
Here is an example of how this error can occur:
DECLARE @counter INT = 0; CONTINUE;
In this example, the code attempts to use the CONTINUE statement outside of a WHILE loop. This will result in SQL Server throwing an error message similar to the following:
Msg 136, Level 15, State 1, Line 3
Cannot use a CONTINUE statement outside the scope of a WHILE statement.

Solution
To resolve this error, ensure that the CONTINUE statement is used within the body of a WHILE loop. For example:
DECLARE @counter INT = 0;
WHILE @counter < 10
BEGIN
SET @counter = @counter + 1;
IF @counter = 5
BEGIN
CONTINUE;
END
PRINT @counter;
END
In this updated example, the code declares a WHILE loop and uses the CONTINUE statement within the loop to skip the iteration when the counter value is equal to 5. The error should no longer occur.