Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE PROCEDURE usp_transfer_money(from_account_id INT,to_account_id INT,money_amount DECIMAL(19,4))
- BEGIN
- START TRANSACTION;
- IF((SELECT id FROM ACCOUNTS WHERE id=from_account_id) IS NULL OR (SELECT id FROM ACCOUNTS WHERE id=to_account_id) IS NULL
- OR money_amount<0 OR (SELECT balance FROM accounts WHERE id=from_account_id)-money_amount<0 OR from_account_id=to_account_id
- )
- THEN
- ROLLBACK;
- ELSE
- UPDATE accounts as a_from
- SET balance=balance-money_amount
- WHERE a_from.id=from_account_id;
- UPDATE accounts as a_to
- SET balance=balance+money_amount
- WHERE a_to.id=to_account_id;
- COMMIT;
- END IF;
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement