4

In Postgresql how can I use JSON_POPULATE_RECORD with an Update query?

1 Answer 1

7

We can use UPDATE query with JSON_POPULATE_RECORD Below is the example for sample table name as a test and having three columns a,b,c with data type character varying.

update test set("a","b","c")=((select a,b,c from json_populate_record(NULL::test,'{"a":"first column","b":"second column","c":"third column"}')))

You can also use UPSERT with this query.

insert into test select * from json_populate_record(NULL::test, '{"a":"first column","b":"column","c":"column"}') ON CONFLICT ON CONSTRAINT a_PKey DO UPDATE SET("a","b","c")=((select a,b,c from json_populate_record(NULL::test,'{"a":"first column","b":"second column","c":"third column"}'))) ;

The above query will insert and if primary key conflicts or constraint conflicts then Record will be Updated.

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.