Đã thêm séc cho số tiền ở đầu quy trình và chuyển insert into transfers trước khi update các câu lệnh. Có khóa ngoại trong transfers tham chiếu bảng account bảng, vì vậy nếu bạn cố gắng chèn id mà không tồn tại nó sẽ thất bại ngay lập tức.
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 ;