tags:

views:

276

answers:

3

I ran into a weird situation today doing some one-time sql code. This nested loop doesn't seem to run the outer loop: it prints (0,0),(0,1),(0,2) and (0,3)

declare @i int, @j int
select @i = 0, @j = 0
while @i < 3 begin
    while @j < 3 begin
        select @i as i, @j as j
        set @j = @j + 1
    end
    set @i = @i + 1
end

Am I missing something blatantly obvious?

+7  A: 

You are not resetting your j var for the next iteration

 set @i = @i + 1
 set @j = 0
almog.ori
Doh! Speaking of obvious!
edosoft
:) they always are
almog.ori
+4  A: 

You are not resetting @j.

ozczecho
A: 

Same mistake. Thanks a lot!

DS-EXPERT