需要开发一个存储过程 ,从 sellorder 表
create table sellorder (
id int primary key auto_increment,
price decimal,
amount int
)
中,按 id 从小到大加载订单数据,生成对账单,写入 bill表
create table bill(
id int primary key auto_increment,
total decimal
)
其中 bill 的 total字段保存了交易单的总金额,这个金额是每一张对账单的所有 sellorder 的 amount 总和,其数值不会超过 20000。
create procedure make_bill()
begin
DECLARE done INT DEFAULT FALSE;
declare cur_sellorder cursor for select id, ,amount from sellorder order by id limit 1000;
declare order_id, amount INT;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open cur_sellorder ;
bill: LOOP
fetch cur_sellorder into order_id, amount;
if done then
LEAVE bill;
end if;
if @total + @amount > 20000 then
leave bill;
else
end if;
END LOOP bill;
CLOSE cur_sellorder ;
start transaction;
delete from sellorder where id < order_id;
commit ;
end;