Saturday 11 May 2019

MSSQL - Fix Error - Cannot use a BREAK statement outside the scope of a WHILE statement.

Msg 135, Level 15, State 1, Line 4
Cannot use a BREAK statement outside the scope of a WHILE statement.


Watch this example on YouTube

To Fix it replace

Declare @Num1 int = 10
If @Num1 = 20
BEGIN
    BREAK
END
While (ISNULL(@Num1, 0) < 10)
BEGIN
    Select GetDate()
END

with

Declare @Num1 int = 10
If @Num1 = 20
BEGIN
    RETURN
END
While (ISNULL(@Num1, 0) < 10)
BEGIN
    Select GetDate()
END

No comments:

Post a Comment