Mar 29, 2014

Cannot use a BREAK statement outside the scope of a WHILE statement.



If we execute following sql query:

DECLARE @Num AS INT = 10
   
IF @Num < 5
    BREAK
ELSE
    SELECT @Num

We will get error message :

Cannot use a BREAK statement outside the scope of a WHILE statement.

Cause: In sql server, keyword BREAK is part of WHILE loop. We must have to use BREAK keyword inside a WHILE loop. For example:

DECLARE @Num AS INT = 10

WHILE @Num  > 0 BEGIN
   
    IF @Num < 5
         BREAK
    ELSE
         SELECT @Num
        
    SET @Num = @Num - 1

END

No comments:

Post a Comment