cholz
cholz

Reputation: 1

PostgreSQL update or insert multiple rows

Quick question regarding "upsert" in PostgreSQL:

INSERT INTO CUSTOMERS (id, name, telephone)
VALUES (1, 'John', '930823088'), (2, 'Laura', '008905238')
ON CONFLICT (id) DO UPDATE

now by adding SET I can set the values, but that would be for the entire table. How do define to set the values per row, so that if id 1 exists, the update inserts the first row of values, if 2 exists, it uses the 2nd row?

Any help is greatly appreciated!

Upvotes: 0

Views: 418

Answers (1)

user330315
user330315

Reputation:

As documented in the manual that's what the excluded pseudo-row is for:

INSERT INTO CUSTOMERS (id, name, telephone)
VALUES (1, 'John', '930823088'), (2, 'Laura', '008905238')
ON CONFLICT (id) DO UPDATE
  set name = excluded.name, 
      telephone = excluded.name;

Upvotes: 3

Related Questions