1

Probably missing something obvious, but I cannot get a nested while loop to function properly. If I comment out the inner while, the outer while works as expected, but otherwise only the inner while is executed. I've culled out the actual code to be executed to try and help figure this out, which is why it's just showing the contents of the variables. I'm sure it's something simple I'm missing but I've been banging my head on this for a while now so hopefully someone can help!

DECLARE @WeekCounter AS int
DECLARE @ClientCounter AS int

SET     @WeekCounter = 1
SET     @ClientCounter = 1

WHILE @ClientCounter <= 3
    BEGIN
        WHILE @WeekCounter <= 2
            BEGIN
                SELECT @ClientCounter,@WeekCounter

                SET @WeekCounter = @Weekcounter + 1
            END

        SET @ClientCounter = @ClientCounter + 1
    END

Thanks

1
  • Never mind, I need to set the variable for the second loop inside the first to reset it... Feeling stupid. Working code - Commented Oct 17, 2017 at 8:17

1 Answer 1

2

Working code -

DECLARE @WeekCounter AS int
DECLARE @ClientCounter AS int

SET     @ClientCounter = 1

---------Set client info from counter--------------
WHILE @ClientCounter <= 3
    BEGIN
        SET @WeekCounter = 1
        WHILE @WeekCounter <= 2
            BEGIN
                SELECT @ClientCounter,@WeekCounter
                SET @WeekCounter = @Weekcounter + 1
            END
        SET @ClientCounter = @ClientCounter + 1
    END
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.