我今天在做一些一次性的sql代码时遇到了一个奇怪的情况。这个嵌套循环似乎没有运行外部循环:它输出(0,0)、(0,1)、(0,2)和(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
我是不是漏掉了什么显而易见的东西?
发布于 2010-01-07 21:34:33
您没有为下一次迭代重置j变量。
set @i = @i + 1
set @j = 0
发布于 2010-01-07 21:36:18
您没有重置@j。
发布于 2012-04-20 21:15:46
declare @i int, @j int
select @i = 0, @j = 0 --<- Wrong place set @j
while @i < 3
begin
select @i, @j --<-test print, then you will know what happened~
--set @j = 0 --<- Right place to set @j
while @j < 3
begin
select @i as i, @j as j
set @j = @j + 1
end
set @i = @i + 1
end
原始结果为0/0 0/0 0/1 0/2 1/3 2/3
好吧,上面的回答是,只需添加代码即可获得更多详细信息,哈哈~
https://stackoverflow.com/questions/2020602
复制相似问题