MySQL: stored procedure

MySQL [test]> delimiter $$
MySQL [test]> create procedure ll()
    -> begin  
    -> declare done int default false;
    -> declare i int default 0;
    -> declare cur cursor for select distinct id from test;
    -> declare continue handler for not found set done = true;
    -> open cur;
    -> lop: LOOP
    -> fetch cur into i;
    -> if done then leave lop;
    -> end if;
    -> select * from test where id = i;
    -> end loop lop ;
    -> end$$

It is not atomic, to start transaction, use start transaction and commit inside it.

create procedure llp()
begin
declare mi bigint default 0;
declare bef bigint default 0;
declare aft bigint default 0;
declare dif bigint default 0;
declare done bigint default false;
declare cur cursor for select * from (select a.mid, a.mistake, b.income_coin as binc, (b.income_coin-a.mistake) as diff from (select dst as mid, sum(coin) as mistake from consume_history where fake = 0 and type in (0,23,25,27,28,29) and unix_timestamp(ct) >= 1596211200 and unix_timestamp(ct) < 1598954400 and dst  in (select distinct mid from withdraw_account_op_record where st = 1596211200 and et = 1598889600 ) and dst not in (1005,1101,1102,1103,1104,1105,1106) and coin > 0 group by dst order by sum(coin) desc) a left join (select  * from withdraw_account) b on a.mid = b.id left join (select * from account ) c on b.id = c.id ) d where diff > 0 order by diff asc;
open cur;
lop: LOOP
fetch cur into mi, dif, bef, aft;
if done then leave lop;
end if;
insert into withdraw_account_op_record(mid, op_type, st, et, pre_value, after_value, ct) values(mi, 0, 0, 0, bef, aft, unix_timestamp(current_timestamp()));
update withdraw_account set income_coin = aft where id = mi;
end loop lop;
end $$

The above sqls are ran after interface offline.  

posted on 2020-09-02 11:06  三叁  阅读(220)  评论(1)    收藏  举报

导航