sql当前行数据和之前行数据相加减循环处理

declare 
@sumQtyBalance decimal(18,2),
@sumOrigValueBalance decimal(18,2),
@QtyBalance  decimal(18,2),
@OrigValueBalance  decimal(18,2),
@rowIndex int
set @sumQtyBalance=0
set @sumOrigValueBalance=0
set @QtyBalance=0
set @OrigValueBalance=0
declare _cursor cursor for(select QtyBalance,OrigValueBalance,rowIndex from TEMP_0_EAP_Rpt_AM_AMDetailRpt4a34ac88_83cd_456f_ac49_a5c300cd3eca )
open _cursor;
fetch next from _cursor into @QtyBalance  ,@OrigValueBalance,@rowIndex  
while @@fetch_status = 0
begin     
  update TEMP_0_EAP_Rpt_AM_AMDetailRpt4a34ac88_83cd_456f_ac49_a5c300cd3eca
  set QtyBalance=ISNULL(@QtyBalance,0)+@sumQtyBalance,OrigValueBalance=ISNULL(@OrigValueBalance,0)+@sumOrigValueBalance
  where rowIndex=@rowIndex
   
  set @sumQtyBalance=ISNULL(@QtyBalance,0)+@sumQtyBalance
  set @sumOrigValueBalance=ISNULL(@OrigValueBalance,0)+@sumOrigValueBalance
   
  fetch next from _cursor into @QtyBalance  ,@OrigValueBalance,@rowIndex  
end
close _cursor
deallocate _cursor

 

posted on 2018-11-20 15:20  苏上话  阅读(1581)  评论(0编辑  收藏  举报