0

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!

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.