BREAK

  • archie flockhart (3/4/2015)


    Curious as to why the Break doesn't exit from the IF , as the referenced BOL page suggests that it should ? ( It clearly does work as stated, just not sure what the BOL reference to the IF ...ELSE statement refers to ? )

    Exits the innermost loop in a WHILE statement or an IF…ELSE statement inside a WHILE loop. Any statements appearing after the END keyword, marking the end of the loop, are executed. BREAK is frequently, but not always, started by an IF test.

    It does exit the IF. The first time round the loop @i is 1 so the IF condition is false, so its IF clause is not entered, and the BREAK is not executed as it's in the IF clause. The code after the end if the IF clause sets the value of @j-2 to 25. The second time round the loop @i is 2 so the IF condition is true, and the the IF clause is entered; the code in the clause first sets @j-2 to 12, then executes BREAK which exits both the IF statement and the innermost loop which contains it.

    Of course this indicates a (trivial) error in the explanation: where it says "from 1" it should have said "from 25".

    Tom

  • sknox (3/4/2015)


    archie flockhart (3/4/2015)


    Curious as to why the Break doesn't exit from the IF , as the referenced BOL page suggests that it should ? ( It clearly does work as stated, just not sure what the BOL reference to the IF ...ELSE statement refers to ? )

    Exits the innermost loop in a WHILE statement or an IF…ELSE statement inside a WHILE loop. Any statements appearing after the END keyword, marking the end of the loop, are executed. BREAK is frequently, but not always, started by an IF test.

    That's some pretty bad grammar, although technically it can be parsed to be correct.

    First note that it says "innermost loop" not "innermost block". So it won't exit an IF statement, because that's not a loop.

    Then "in a WHILE statement or an IF…ELSE statement inside a WHILE loop" indicates where a BREAK can be located, not what it acts upon.

    I agree that that's what it was intended to mean, but I think you are being over-generous to the BOL page author when you say that technically it can be parsed technically it can be parsed to be correct. It really needs fixing, perhaps by inserting the words "when exectuted" after the first occurrence of "loop".

    edit: fix quote tags

    Tom

  • Thanks, Steve! Great question.

    - webrunner

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

Viewing 3 posts - 16 through 17 (of 17 total)

You must be logged in to reply to this topic. Login to reply