游标,反复执行
写了一段语句,主要修改指定商品,指定日期的数据,但是现在想修改一时间段里的数据,请问怎么修改。
@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
@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
...全文
相关推荐
MySQL存储过程中游标循环的跳出和继续操作示例
首先,游标循环通常有三种基本形式:`LOOP`、`REPEAT`和`WHILE`。每种循环结构都有其特定的语法和用途,但核心思想都是根据一定的条件反复执行一段代码。1.
游标卡尺的使用flash动画演示教学课件说课讲解.ppt
此外,通过动画演示,学习者可以反复观看和模拟操作,加深记忆和理解。
检测设备内校规程汇编游标卡尺内部校准规程千分尺内部校验规程.docx
正确执行这些规程,结合持续的培训和设备维护,我们能够有效地提高工作效率,降低不良品率,最终实现企业利益的最大化。
sql-Cursor高效代码编写技巧
通过反复审查代码逻辑和执行性能测试,可以发现和修正可能导致性能问题的代码段落。应当定期执行这样的审查和测试,确保随着数据库的增长和变更,Cursor的代码仍然保持高效。
存储过程小总结
**动态定义游标**:游标用于逐行处理查询结果。在动态定义游标时,我们可以创建一个SQL字符串,然后使用`EXEC`执行它来打开游标。7.


应用实例
