Eine Überprüfung des Betrags zu Beginn des Verfahrens hinzugefügt und insert into transfers verschoben vor update Aussagen. Bei transfers gibt es Fremdschlüssel Tabelle, die auf account verweist Tabelle, wenn Sie also versuchen, id einzufügen das nicht existiert, wird sofort fehlschlagen.
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 ;