September 14, 2005 at 12:01 pm
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
September 14, 2005 at 12:08 pm
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
September 14, 2005 at 12:19 pm
Is this some homework or you need this to go on a production server??
September 14, 2005 at 12:20 pm
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.
September 14, 2005 at 12:27 pm
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
September 14, 2005 at 12:45 pm
Thanks for the insights!! I'm screwing around learning....Thanks a million!
September 14, 2005 at 12:51 pm
HTH.
September 14, 2005 at 3:01 pm
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.
September 14, 2005 at 3:09 pm
SET @Count = 1
...
while @Count = 3
since when does 1=3???
September 14, 2005 at 3:12 pm
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 !!!**
September 14, 2005 at 3:15 pm
Very Funny
* Noel
September 14, 2005 at 3:19 pm
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.
September 14, 2005 at 3:46 pm
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