Advertisement
Valeri12580

14 database programmabillity

Feb 7th, 2020
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. CREATE PROCEDURE usp_transfer_money(from_account_id INT,to_account_id INT,money_amount DECIMAL(19,4))
  2. BEGIN
  3. START TRANSACTION;
  4. IF((SELECT id FROM ACCOUNTS WHERE id=from_account_id) IS NULL OR (SELECT id FROM ACCOUNTS WHERE id=to_account_id) IS NULL
  5. OR money_amount<0 OR (SELECT balance FROM accounts WHERE id=from_account_id)-money_amount<0 OR from_account_id=to_account_id
  6. )
  7. THEN
  8. ROLLBACK;
  9. ELSE
  10. UPDATE accounts as a_from
  11. SET balance=balance-money_amount
  12. WHERE a_from.id=from_account_id;
  13.  
  14. UPDATE accounts as a_to
  15. SET balance=balance+money_amount
  16. WHERE a_to.id=to_account_id;
  17.  
  18. COMMIT;
  19. END IF;
  20.  
  21.  
  22.  
  23. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement