Merge into does not work

From: Shaozhong SHI <shishaozhong(at)gmail(dot)com>
To: pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Merge into does not work
Date: 2021-11-26 19:08:24
Message-ID: CA+i5JwZoUVr6CW4F2gCSUMjLHDXOrTWZ6=xwzsXwCjJtKnomtA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

CREATE TABLE Stock(item_id int UNIQUE, balance int);

INSERT INTO Stock VALUES (10, 2200);
INSERT INTO Stock VALUES (20, 1900);

CREATE TABLE Buy(item_id int, volume int);

INSERT INTO Buy values(10, 1000);
INSERT INTO Buy values(30, 300);

MERGE INTO Stock USING Buy ON Stock.item_id = Buy.item_id
WHEN MATCHED THEN UPDATE SET balance = balance + Buy.volume
WHEN NOT MATCHED THEN INSERT VALUES (Buy.item_id, Buy.volume);

I am using Postgres 9.6.

Regards,

David

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2021-11-26 19:11:14 Re: Merge into does not work
Previous Message Tom Lane 2021-11-26 18:42:16 Re: What do you do with a long running rollback