Nested Break

  • Steve Jones - SSC Editor

    SSC Guru

    Points: 721105

    Comments posted to this topic are about the item Nested Break

  • Hany Helmy

    SSChampion

    Points: 13488

    Interesting question, never user [BREAK] before, thanx

  • patricklambin

    SSCrazy Eights

    Points: 9964

    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.

  • Igor Micev

    SSC-Dedicated

    Points: 33109

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

    Igor Micev,
    My blog: www.igormicev.com

  • Carlo Romagnano

    SSC-Insane

    Points: 22015

    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

  • Koen Verbeeck

    SSC Guru

    Points: 258985

    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

  • redtux1

    Old Hand

    Points: 320

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

  • Ed Wagner

    SSC Guru

    Points: 286987

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

  • x

    SSC-Insane

    Points: 23595

    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 😉

  • akljfhnlaflkj

    SSC Guru

    Points: 76202

    I have never used BREAK.

  • mwpowellhtx

    Old Hand

    Points: 356

    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.

  • paul.goldstraw

    SSCrazy

    Points: 2626

    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

  • mwpowellhtx

    Old Hand

    Points: 356

    Well, I stand corrected. Thank you. 🙂

  • ZZartin

    SSC-Dedicated

    Points: 30420

    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 22 total)

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