Se agregó un cheque por el monto al comienzo del procedimiento y se movió insert into transfers
antes de update
declaraciones. Hay claves foráneas en transfers
tabla que hace referencia a account
tabla, por lo que si intenta insertar id
que no existe fallará inmediatamente.
delimiter //
create procedure transfer (amount int, note varchar(50), sending_account
int, receiving_account int)
this_proc:begin
start transaction;
if amount <= 0 then
leave this_proc;
end if;
insert into Transfers values
(TransfersID, amount, sending_account, receiving_account, note, now());
update Account as A
set A.amount = A.amount - amount
where A.AccountID = sending_account;
update Account as A
set A.amount = A.amount + amount
where A.AccountID = receiving_account;
commit work;
end //
delimiter ;