jameschg
jameschg

Reputation: 39

TSQL Nested loop only executes inner loop

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

Upvotes: 1

Views: 588

Answers (1)

jameschg
jameschg

Reputation: 39

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

Upvotes: 2

Related Questions