In Postgresql how can I use JSON_POPULATE_RECORD with an Update query?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With