Sanket Patel
Sanket Patel

Reputation: 931

How can i use update with json populate record in postgresql?

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

Upvotes: 4

Views: 4326

Answers (1)

Sanket Patel
Sanket Patel

Reputation: 931

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.

Upvotes: 7

Related Questions