March 17, 2015 at 12:03 am
Comments posted to this topic are about the item Nested Break
March 17, 2015 at 12:47 am
Interesting question, never user [BREAK] before, thanx
Thanks & Best Regards,
Hany Helmy
SQL Server Database Consultant
March 17, 2015 at 1:25 am
Good question but an easy one.
I have appreciated because it is an example of "bad code" ( too difficult to understand ) that I have met too many times even from experimented DBAs.
Thanks Steve.
March 17, 2015 at 1:53 am
Some questions for using BREAK and CONTINUE. Good. Functions behave the same as in programming languages.
Igor Micev,My blog: www.igormicev.com
March 17, 2015 at 2:16 am
patricklambin (3/17/2015)
Good question but an easy one.I have appreciated because it is an example of "bad code" ( too difficult to understand ) that I have met too many times even from experimented DBAs.
Thanks Steve.
+1
March 17, 2015 at 2:26 am
Easy one and a bit of a déjà vu as well.
Thanks Steve for the question.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
March 17, 2015 at 2:37 am
This was removed by the editor as SPAM
March 17, 2015 at 5:33 am
This was a great example of working through code that should not be used. Nice for a question, though. Thanks.
March 17, 2015 at 6:52 am
Doesn't work for me, I get
Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "@j".
I'm pretty sure that when the code / batch completes, all variables become undefined 😉
March 17, 2015 at 6:57 am
I have never used BREAK.
March 17, 2015 at 7:52 am
It is an interesting exercise, but doesn't really accomplish anything anyone would encounter in the real world. BREAK is an interesting SQL keyword, did not know that. Also beware the @k red-herring-of-sort; one would want to wonder if a logical reduction couldn't (shouldn't) occur.
March 17, 2015 at 8:59 am
mwpowellhtx (3/17/2015)
It is an interesting exercise, but doesn't really accomplish anything anyone would encounter in the real world. BREAK is an interesting SQL keyword, did not know that. Also beware the @k red-herring-of-sort; one would want to wonder if a logical reduction couldn't (shouldn't) occur.
On the contrary, I use it reasonably often when I need looping logic for administrative purposes or for complex row-by-row logic.
Structure is typically as follows
WHILE 1 = 1 --Always true, so enter the loop
BEGIN
SELECT TOP 1
@CurrentRecordID = RecordID
FROM TableName
WHERE RecordID > @CurrentRecordID --Get the next record
ORDER BY RecordID
IF @@ROWCOUNT = 0
BEGIN
--No rows left so break out of loop
BREAK
END
ELSE
BEGIN
--perform looping logic
PRINT @CurrentRecordID
END
END
March 17, 2015 at 9:10 am
Well, I stand corrected. Thank you. 🙂
Viewing 15 posts - 1 through 15 (of 21 total)
You must be logged in to reply to this topic. Login to reply