Menambahkan cek untuk jumlah di awal prosedur, dan memindahkan insert into transfers
sebelum update
pernyataan. Ada kunci asing di transfers
tabel referensi account
tabel, jadi jika Anda mencoba memasukkan id
yang tidak ada akan langsung gagal.
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 ;