Nested while is not working

  • PLease find my code here.

    begin

    declare @i int,@j int,@name varchar(20)

    SET @i = 5

    SET @j-2 = 3

    SET @name ='Prabhakaran'

    WHILE @i > 0

    BEGIN

    WHILE @j-2>0

    BEGIN

    Print @name

    Print @j-2

    SET @j-2 = @j-2 - 1

    END

    SET @i = @i -1

    END

    End

  • You should tell us why you'll need nested While loops... typically, that's a recipe for a huge RBAR performance problem and it can usually be avoided. 😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • You need to include the intial value of @j-2 in the @I loop or it just counts down in one iteration of @I and stays there.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (10/13/2008)


    Last, but not least, you need to include the intial value of @j-2 in the @I loop or it just counts down in one iteration of @I and stays there.

    Agreed - that's more than likely where the confusion is.

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • hi,

    your code is working perfect.

    you set the variable value to it's condition then what to do.

    SET @i = 5

    SET @j-2 = 3

    SET @name ='test'

    WHILE @i > 0

    BEGIN

    print @i

    WHILE @j-2>0

    BEGIN

    Print @name + '-' + cast(@j as varchar)

    --here you set j value b'cz it's global variable

    SET @j-2 = @j-2 - 1

    END

    --now here set and check code

    SET @j-2 = 3

    SET @i = @i -1

    END

    End

    if i m getting right then?

    thnx

  • Still, the code is doomed to be comparatively slow because of the loops. If we knew the actual problem this was supposed to solve, perhaps we could find a proper set based solution for the problem. 😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • By this time I think the PO is not longer with us (no pun intended)


    * Noel

  • noeld (10/14/2008)


    By this time I think the PO is not longer with us (no pun intended)

    Heh, agreed. It's a shame, too. I would have liked to take a crack at whatever they thought they needed a double loop for... maybe even if it was homework.:P

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 8 posts - 1 through 7 (of 7 total)

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