March 4, 2015 at 1:38 am
Heh... the correct answer is @j. 😛
Great question, BTW.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 4, 2015 at 2:26 am
Nice question, thanks Steve.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
March 4, 2015 at 4:24 am
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.
March 4, 2015 at 5:06 am
I gave the correct answer, but I am confused as well when I read BOL after that 🙂
March 4, 2015 at 5:18 am
I know it's in SQL, but it reminds me of an intro to programming class in school years ago. I still remember that approach, no matter how hard I try to forget.
March 4, 2015 at 5:29 am
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.
March 4, 2015 at 6:38 am
This is the first time the explanation confused me more than the question. 🙂 Thanks for the clarification, sknox!
Nice one, Steve.
March 4, 2015 at 7:17 am
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.
I inferred the exact same thing from the MSDN site https://msdn.microsoft.com/en-us/library/ms181271.aspx. Why even mention the IF...ELSE statement if it's always going to exit the loop anyway?
Be still, and know that I am God - Psalm 46:10
March 4, 2015 at 7:54 am
The explanation of sknox makes sense, at least given the empirical evidence from running the code. Still, the documentation does seem decidedly poor in that it takes almost legalistic reasoning to discern the actual behavior from it.
March 4, 2015 at 8:28 am
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 fooled me as well. What it actually does makes more sense, but the above section from BOL makes it sound like it's going to break out of the IF statement.
March 4, 2015 at 8:29 am
david.gugg (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.
I inferred the exact same thing from the MSDN site https://msdn.microsoft.com/en-us/library/ms181271.aspx. Why even mention the IF...ELSE statement if it's always going to exit the loop anyway?
Good question.
March 4, 2015 at 8:55 am
It broke out farther back than I thought it would.
March 4, 2015 at 9:37 am
Scored 1 point. Thanks for the quick brain teaser, Steve!
Thanks.
Viewing 15 posts - 1 through 15 (of 17 total)
You must be logged in to reply to this topic. Login to reply