我想知道为什么 while 循环内的表变量的行为与其他变量不同。表变量仅创建一次,并将在整个循环中使用。但每次循环增加时其他变量都会被初始化。
查看下面的代码以获取更多信息
declare @tt int
set @tt =10
while @tt>0
begin
declare @temptable table(id int identity(1,1),sid bigint)
insert into @temptable
select @tt union all
select @tt + 1
select * from @temptable
--delete from @temptable
set @tt=@tt-1
end
这是一个错误吗?
你的前提是错误的。其他变量也不会在每次遇到声明语句时重新初始化。
set nocount on
declare @tt int
set @tt =10
while @tt>0
begin
declare @i int
set @i = isnull(@i,0) + 1
print @i
set @tt=@tt-1
end
Prints
1
2
...
9
10
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)