In the SQL Server 2012 stored procedure, I have several nested structures. I want to break out of one layer of them.
I thought the BREAK description in msdn https://msdn.microsoft.com/en-CA/library/ms181271.aspx was on my side. But I get some weird behavior when it starts in one step using debugging. I speak strange because it is consistent. Sometimes it eludes the layer that I expect. Sometimes he skips a couple.
WHILE ... BEGIN stuff1 IF...BEGIN stuff2 WHILE ... BEGIN stuff3 IF .... BEGIN stuff4 IF @NumberRecords=0 BREAK stuff5 END --stuff6 if @NumberRecords=0 and @ loopBOMRowCount=@ResultsSOloopstart-1 break --on the last occasion I observed, @loopBOMRowCount was 6 and @ResultsSOloopstart 71 and it never highlighted this section, either way SET @loopBOMRowCount = @loopBOMRowCount + 1 END stuff7 --nothing actually here END --stuff8 SET @ periodloopcount=@periodloopcount +1 --this is where it ended up highlighting on that last occasion END stuff9
So, if NumberRecords = 0, then the next op should be if if on stuff6, right? Even if stuff4 includes, say, an INSERT INTO table from an EXEC call into a stored procedure? Nothing should confuse a stack of its layers?
And yes, I understand that ugly SQL. Most instructions are edited in two temporary tables, and I avoided passing them back and forth to stored procedures that would otherwise clear the code.
EDIT
I was able to set up the route the way I wanted by adding a dummy WHILE loop around the inner IF, which I want to break out of the first. But I would love to know how I misinterpret msdn information. It seems BREAK should exit IF if it has an END statement.
This leaves the innermost loop in the WHILE statement or the IF ... ELSE statement inside the WHILE loop. Any statements appearing after the END keyword that indicates the end of the loop are executed.
source share