读书人

关于while循环

发布时间: 2013-12-29 13:07:03 作者: rapoo

求助关于while循环
例如:
while(@id >0)--@id大于500
begin
select @i= i,@x=x from tba
where id=@id
while(@i>10) --@i 基本都大于20
begin
update tbb
set z=z+1
where tbb.x=@x --原代码中的update要多表连接

set @i =@i-1
end
set @id=@id-1
end

这样的循环怎么改能提升他的执行速度呢
[解决办法]
其实很多循环是可以集合操作来优化,不过你这个查询是用来干嘛的?
[解决办法]
其实只需要这个就行了,然后把上面的语句合并进去:

update #order
set 一个月=isnull(#order.一个月,0)+A.一个月
from
(
select T_顾客.顾客, sum((isnull(dbo.T_顾客详细.金,0))) as 一个月
from dbo.T_顾客
where dbo.T_顾客.工厂区分='1' and 受注日 <=@末
and 受注日>=@前
group by T_顾客.顾客
)A
where #order.顾客=@cust

读书人网 >SQL Server

热点推荐