27,579
社区成员
发帖
与我相关
我的任务
分享
declare @oper_date varchar(10), @oper_date2 varchar(10)
@item_no varchar(20),
@branch_no varchar(2),
@sale_amt numeric(16,4)
select @oper_date = '2009-08-01',@oper_date2 ='2009-08-31'
select @item_no = '40002'
declare lcur_branch cursor for
select distinct left(branch_no,2)
from t_rm_daysum
where oper_date between @oper_date and @oper_date2 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 between @oper_date and @oper_date2
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 between @oper_date and @oper_date2
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 between @oper_date and @oper_date2 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
TRY.
fetch next lcur_branch into @branch_no --这个也要修改
declare --@oper_date varchar(10),
@item_no varchar(20),
@branch_no varchar(2),
@sale_amt numeric(16,4),
@start datetime, --新增2个
@end datetime --新增
select @start='2009-08-01' --新增
select @end='2009-08-31' --新增
--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 between @start and @end and item_no = @item_no --修改
--下面继续
......
end
fetch next from lcur_branch into @branch_no
end
close lcur_branch
deallocate lcur_branch
fetch lcur_branch into @branch_no
修改
fetch next from lcur_branch into @branch_no