Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # CREATE TABLE
- CREATE TABLE accounts (
- id INT GENERATED BY DEFAULT AS IDENTITY,
- name VARCHAR(100) NOT NULL,
- balance DEC(15,2) NOT NULL,
- PRIMARY KEY(id)
- );
- # INSERT
- INSERT INTO accounts(name,balance) VALUES
- ('Bob',10000), ('Alice',10000);
- # STORED PROCEDURE
- CREATE OR REPLACE PROCEDURE transfer(INT, INT, DEC)
- LANGUAGE plpgsql
- AS $$
- BEGIN
- -- subtracting the amount from the sender's account
- UPDATE accounts
- SET balance = balance - $3
- WHERE id = $1;
- -- adding the amount to the receiver's account
- UPDATE accounts
- SET balance = balance + $3
- WHERE id = $2;
- COMMIT;
- END;
- $$;
- # Controller
- class Account extends CI_Controller {
- public function index() {
- $stored_procedure = 'CALL transfer(?, ?, ?)';
- $parameter = [1, 2, 1000];
- $result = $this->db->query($stored_procedure, $parameter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement