Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2004
    Posts
    3

    Unanswered: 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:
    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

    [edit]corrected typo...[/edit]
    Last edited by drunken-wallaby; 12-10-04 at 08:04.

  2. #2
    Join Date
    Feb 2004
    Posts
    492
    Quote Originally Posted by drunken-wallaby
    [code]
    SET i = @i + 1
    /code]
    try:
    Code:
      SET @i = @i + 1

  3. #3
    Join Date
    Dec 2004
    Posts
    3
    Quote Originally Posted by Kaiowas
    try:
    Code:
     SET @i = @i + 1
    Hi 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

  4. #4
    Join Date
    Feb 2004
    Posts
    492
    there might be another thing:
    once @j reaches 100, it is not reset again, so the inner loop runs once.

  5. #5
    Join Date
    Dec 2004
    Posts
    3
    Quote Originally Posted by Kaiowas
    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 I have to reset the inner counter variable for sure. The correct code:
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •