Advertisement
fbinnzhivko

15 zadacha

Jun 19th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 0.85 KB | None | 0 0
  1. CREATE PROCEDURE usp_transfer_money(from_account_id INT, to_account_id INT, amount DECIMAL(19,4))
  2. BEGIN
  3.     START TRANSACTION;
  4.         UPDATE accounts SET balance = balance - amount
  5.             WHERE id = from_account_id;
  6.             UPDATE accounts SET balance = balance + amount
  7.             WHERE id = to_account_id;
  8.            
  9.         IF((SELECT COUNT(*) FROM accounts
  10.               WHERE id = from_account_id) <> 1)
  11.            THEN ROLLBACK;
  12.         ELSEIF(amount > (SELECT balance FROM accounts WHERE id = from_account_id))
  13.             THEN ROLLBACK;
  14.         ELSEIF(amount <= 0)
  15.             THEN ROLLBACK;
  16.         ELSEIF((SELECT balance FROM accounts WHERE id = from_account_id) <= 0)
  17.             THEN ROLLBACK; 
  18.         ELSEIF((SELECT COUNT(*) FROM accounts
  19.               WHERE id = to_account_id) <> 1)
  20.            THEN ROLLBACK;
  21.         ELSEIF(amount <= 0)
  22.             THEN ROLLBACK;
  23.         ELSEIF(from_account_id = to_account_id)
  24.             THEN ROLLBACK;
  25.         ELSE
  26.             COMMIT;
  27.         END IF;
  28.  
  29. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement