Nested Break

  • Comments posted to this topic are about the item Nested Break

  • Interesting question, never user [BREAK] before, thanx

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • 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.

  • Some questions for using BREAK and CONTINUE. Good. Functions behave the same as in programming languages.

    Igor Micev,My blog: www.igormicev.com

  • 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

  • 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

  • This was removed by the editor as SPAM

  • Plus of course @k never gets alters (which @j-2 is dependent on)

  • This was a great example of working through code that should not be used. Nice for a question, though. Thanks.

  • 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 😉

  • I have never used BREAK.

  • 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.

  • 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

  • Well, I stand corrected. Thank you. 🙂

  • So was this a question on why single character variable names @i and @j-2 are terrible choices?

Viewing 15 posts - 1 through 15 (of 21 total)

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