Friday, March 23, 2012

nested while loop

Hi there. I want to avoid a cursur using a quite basing nested while loop. The problem is, that the outer index-variable (i) won't increment at all while the inner loop works perfectly.

This one should be quite easy to solve I guess, I'd be very happy if someone could give me a hint what I should try, though because I don't know what to try. The manual didn't help me much either, using CONTINUE and BREAKs didn't solve this problem for me.

My code:
DECLARE @.i INTEGER
DECLARE @.j INTEGER
SET @.i = 1
SET @.j = 0

WHILE(@.i<= 10) BEGIN
WHILE(@.j <= 100) BEGIN
SELECT @.i, @.j, COUNT(*) as anz FROM mytable WHERE dim1 = @.i AND dim2 = @.j
SET @.j = @.j + 1
END
SET @.i = @.i + 1
END

Thanks a lot for your help :)
Bernhard

corrected typo...[code]
SET i = @.i + 1
/code]

try:

SET @.i = @.i + 1|||try: SET @.i = @.i + 1Hi Kaiowas. Thanks for your reply, but that was just a silly typo in my posting. The original code ist just semantically correct as you suggested. It's not working, though...

Greets, Bernhard|||there might be another thing:
once @.j reaches 100, it is not reset again, so the inner loop runs once.|||there might be another thing:
once @.j reaches 100, it is not reset again, so the inner loop runs once.

Thanks a lot Kaiowas. Silly me :mad: I have to reset the inner counter variable for sure. The correct code:
DECLARE @.i INTEGER
DECLARE @.j INTEGER
SET @.i = 1
SET @.j = 0

WHILE(@.i<= 10) BEGIN
WHILE(@.j <= 100) BEGIN
SELECT @.i, @.j, COUNT(*) as anz FROM mytable WHERE dim1 = @.i AND dim2 = @.j
SET @.j = @.j + 1
END
SET @.j = 0
SET @.i = @.i + 1
END

Thanks a lot :)
Bernhard

No comments:

Post a Comment