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 INTO accounts(name,balance)
VALUES('Bob',10000);
INSERT INTO accounts(name,balance)
VALUES('Alice',10000);
select * from accounts;
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;
$$;
call transfer(1,2,1000);
select * from accounts;
参考:
添加链接描述