Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. # CREATE TABLE
  2. CREATE TABLE accounts (
  3. id INT GENERATED BY DEFAULT AS IDENTITY,
  4. name VARCHAR(100) NOT NULL,
  5. balance DEC(15,2) NOT NULL,
  6. PRIMARY KEY(id)
  7. );
  8.  
  9. # INSERT
  10. INSERT INTO accounts(name,balance) VALUES
  11. ('Bob',10000), ('Alice',10000);
  12.  
  13. # STORED PROCEDURE
  14. CREATE OR REPLACE PROCEDURE transfer(INT, INT, DEC)
  15. LANGUAGE plpgsql
  16. AS $$
  17. BEGIN
  18. -- subtracting the amount from the sender's account
  19. UPDATE accounts
  20. SET balance = balance - $3
  21. WHERE id = $1;
  22.  
  23. -- adding the amount to the receiver's account
  24. UPDATE accounts
  25. SET balance = balance + $3
  26. WHERE id = $2;
  27.  
  28. COMMIT;
  29. END;
  30. $$;
  31.  
  32. # Controller
  33. class Account extends CI_Controller {
  34. public function index() {
  35. $stored_procedure = 'CALL transfer(?, ?, ?)';
  36. $parameter = [1, 2, 1000];
  37.  
  38. $result = $this->db->query($stored_procedure, $parameter);
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement