游标,反复执行

fancydong fancydong 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
写了一段语句,主要修改指定商品,指定日期的数据,但是现在想修改一时间段里的数据,请问怎么修改。
@oper_date指定的日期@item_no指定的商品 ,现在想修改一段时间的,例如8月1日至8月31日

declare @oper_date varchar(10),
@item_no varchar(20),
@branch_no varchar(2),
@sale_amt numeric(16,4)

select @oper_date = '2009-08-26'
select @item_no = '40002'

declare lcur_branch cursor for
select distinct left(branch_no,2) from t_rm_daysum where oper_date = @oper_date and item_no = @item_no

open lcur_branch
fetch lcur_branch into @branch_no
while @@fetch_status = 0
begin
--查找金额
select @sale_amt = 0
select @sale_amt = sum(sale_amt - ret_amt) from t_rm_daysum where item_no = @item_no and oper_date = @oper_date and left(branch_no,2) = @branch_no

--找最大金额来更新金额
if (@sale_amt is not null) and @sale_amt > 0
begin
update t_rm_casher_daysum set real_amt = real_amt - @sale_amt where oper_date = @oper_date and branch_no = left(@branch_no, 2)
and sale_way = 'A' and pay_way = 'RMB' and real_amt > @sale_amt
and casher_no = (select top 1 casher_no from t_rm_casher_daysum where oper_date = @oper_date and branch_no = left(@branch_no, 2)
and sale_way = 'A' and pay_way = 'RMB' and real_amt > @sale_amt order by real_amt desc)
end

fetch lcur_branch into @branch_no
end

close lcur_branch
deallocate lcur_branch
...全文

相关推荐

评论 18
收藏 已收藏
分享
返回顶部
fancydong
应用实例 27,579 成员 68,540 内容