mark79

Deposit Money

Jan 19th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 0.43 KB | None | 0 0
  1. CREATE PROCEDURE usp_deposit_money(account_id INT, money_amount DECIMAL(19, 4))
  2. BEGIN
  3.     DECLARE from_account INT;
  4.  
  5.     SET from_account := (
  6.         SELECT count(*)
  7.         FROM accounts
  8.         WHERE accounts.id = account_id
  9.     );
  10.  
  11.     START TRANSACTION;
  12.            
  13.     IF (money_amount < 0 OR from_account = 0) THEN
  14.         ROLLBACK;
  15.     ELSE   
  16.         UPDATE accounts
  17.         SET balance = balance + money_amount
  18.         WHERE accounts.id = account_id;
  19.     END IF;
  20. END;
Add Comment
Please, Sign In to add comment