simple while loop not printing out value

  • can somebody tell me why the code below will not spit out the number values?

     

    Declare @count int

    set @Count = 1

    while @Count = 3

      Begin

              set @Count = @Count + 1

              print @Count

      End

  • first of all you set count = 1 and count never got to the while loop of count = 3 so that's why it never print. 

     

    try another way:

    --------

    Declare @count int

    set @Count = 1

    while @Count < 5

      Begin

            set @Count = @Count + 1

            if (@count = 3) print @Count

     

      End

     

     

    mom

  • Is this some homework or you need this to go on a production server??

  • I thought the same thing, but try this:

    DECLARE @Count integer

    SET @Count = 1

    WHILE @Count < 3

    BEGIN

             SET @Count = @Count + 1

             PRINT @Count

    END

    Output

    2

    3

    I think it is the equality.  The only time I could find in BOL an equality with a WHILE was: WHILE( @@FETCH_STATUS = 0).  WHILE does not seem to be able to handle a staight equality, hence your use of an IF statement...

    I cannot answer the question, but it does pose an interesting conundrum. 

    I wasn't born stupid - I had to study.

  • Hey bull, how you doing ??

    DECLARE @Count as integer

    DECLARE @Done as bit

    SET @Count = 0

    SET @Done = 0

    WHILE @Done = 0

    BEGIN

    SET @Count = @Count + 1

    IF @Count = 3

    BEGIN

    SET @Done = 1

    END

    PRINT @Count

    END

  • Thanks for the insights!! I'm screwing around learning....Thanks a million!

  • HTH.

  • Hi Remi - busy...

    While that works, it does not answer the question. 

    Why is WHILE @Count = 3 not PRINTING?  It requires an IF to work...

    I wasn't born stupid - I had to study.

  • SET @Count = 1

    ...

    while @Count = 3

    since when does 1=3???

  • Farrell - maybe it's time to hit the pubs again - thirst can do this to a man!

    editing - pubs - as in thirstquenchers - NOT databases...







    **ASCII stupid question, get a stupid ANSI !!!**

  • Very Funny


    * Noel

  •   Like I said, I wasn't born stupid...  I have to work at this!!!! 

    I am too involved in something else and wasn't thinking...  Glad to be the butt of humor on this one...  

    Enjoy 

    I wasn't born stupid - I had to study.

  • Oh we are enjoying .

Viewing 13 posts - 1 through 12 (of 12 total)

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