drop trigger BalanceNotice;
create trigger BalanceNotice
after insert on trade_details for each row
#when NEW.t_result=1
Begin
declare v_phone varchar(30);
declare v_type varchar(30);
declare v_bal int;
declare v_msg varchar(300);
select phone_no,a_bal into v_phone,v_bal from accounts where a_no=new.a_no;
if new.t_type=1 then
set v_type ='存款';
set v_bal=new.t_amt+v_bal;
UPDATE accounts set a_bal=v_bal where a_no=new.a_no;
end if;
if new.t_type=2 then
set v_type ='取款';
set v_bal=v_bal-new.t_amt;
UPDATE accounts set a_bal=v_bal where a_no=new.a_no;
end if;
set v_msg =concat(new.t_date,'你的账户',new.a_no,'上执行了',v_type,'交易,存款金额:',new.t_amt,',账号余额为: ',v_bal);
insert into log values(v_msg);
end
sql复制代码
drop procedure branceBalancesum
create procedure branceBalancesum(in s_date VARCHAR(20))
Begin
declare all_balance int;
declare v_bran_no varchar(10);
declare v_bran_name varchar(30);
declare v_finished int;
declare c_sum_bal CURSOR FOR select open_branch,open_branch_name,sum(a_bal) from accounts GROUP BY open_branch,open_branch_name;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;
open c_sum_bal;
process_loop: loop
fetch c_sum_bal into v_bran_no,v_bran_name,all_balance ;
if v_finished=1 then
LEAVE process_loop;
end if ;
insert into branch_sum values(v_bran_no,s_date,v_bran_name,all_balance);
end loop;
CLOSE c_sum_bal;
commit;
END;
call branceBalancesum('2025-02-07')