猿问

百万数据库循环取值进行更新的算法

begin

Declare GuestCursor Cursor For
Select top 88888 XLH,SL from CP_SSQ6_005 where xtbz='0' order by XLH asc;
Open GuestCursor
Fetch Next From GuestCursor into @V_XLH,@V_SL;
WHILE @@FETCH_STATUS = 0
Begin
update CP_SSQ6_005 set xtbz='1' where xlh=@V_XLH;

set @V_SL=rtrim(ltrim(@V_SL));
set @V_1=substring(@V_SL,1,2);
set @V_2=substring(@V_SL,3,2);
set @V_3=substring(@V_SL,5,2);
set @V_4=substring(@V_SL,7,2);
set @V_5=substring(@V_SL,9,2);
set @V_6=substring(@V_SL,11,2);

Select @V_or=dbo.F_GetOrder_2_3(@V_SL)

set @T_1=CAST(@V_1 AS decimal(18,0))
set @T_2=CAST(@V_2 AS decimal(18,0))
set @T_3=CAST(@V_3 AS decimal(18,0))
set @T_4=CAST(@V_4 AS decimal(18,0))
set @T_5=CAST(@V_5 AS decimal(18,0))
set @T_6=CAST(@V_6 AS decimal(18,0))

set @T_JE=@T_1+@T_2+@T_3+@T_4+@T_5+@T_6;



if(@T_JE>=121)
begin

update CP_SSQ6_005 set scbz='1',xtbz='1' where xlh=@V_XLH;
waitfor delay '00:00:00';

--
end;


Fetch Next From GuestCursor into @V_XLH,@V_SL;
end
Close GuestCursor
DealLocate GuestCursor
end

 

我有一个百万数据库,需要循环取值进行计算,现在是用游标每次只能取88888 条记录,这样很麻烦,有没有百万数据库循环取值进行更新的算法

手掌心
浏览 610回答 3
3回答

哈士奇WWW

可以试下把判断逻辑放到一个用户函数中,然后批量更新值,如 update CP_SSQ6_005 set scbz='1',xtbz='1' where dbo.fun(SL)>=121

慕哥9229398

为什么要用游标,直接循环就可以了, update CP_SSQ6_005 set xtbz='1' where xlh=@V_XLH; xlh是索引吗?可以直接用物理的ID. update CP_SSQ6_005 set scbz='1',xtbz='1' where  两次更新xtbz='1' 为同样的值?   @V_1 @T_1变量可以提出循环

holdtom

不建议使用游标,严重影响效率
随时随地看视频慕课网APP

相关分类

SQL Server
我要回答