0
点赞
收藏
分享

微信扫一扫

sql server 查出select一批数据并update 语句写法


需求:从sql server数据库中,先查出一批数据,然后遍历,修改一点信息。
现后用了两种方法,差异极大。
性能相差至少上千倍,特此记录。

以下是记录:

存储过程:

//性能低
declare @id int,@orderId nvarchar(100);
begin
declare my_cursor cursor fast_forward for (select e.ID,e.OrderID from eInvoice e where 。。。。);
open my_cursor;--打开游标
while 1=1 --开始循环
begin
fetch next from my_cursor into @id,@orderId; --赋值到变量中
if(@@fetch_status!=0)break;--如果没有结果退出循环
update eInvoice set BlueInvoiceId = 0 where OrderId = @orderId;
end
close my_cursor --关闭游标
deallocate my_cursor --释放游标
end

//性能极高
update a set a.OrderPrice=b.PayPrice from eInvoice a,PaymentOrder b where a.KpType=1 and a.OrderID=b.TableId ;


举报

相关推荐

0 条评论