Re: update without SET ?

Поиск
Список
Период
Сортировка
От David G. Johnston
Тема Re: update without SET ?
Дата
Msg-id CAKFQuwY7=QFQSkpcEPbrYpRv_4ZCJwWa9HZxv_X5UYXzRkmmbA@mail.gmail.com
обсуждение исходный текст
Ответ на update without SET ?  (Michael Moore <michaeljmoore@gmail.com>)
Ответы Re: update without SET ?  ("David G. Johnston" <david.g.johnston@gmail.com>)
Список pgsql-sql
On Wed, Feb 3, 2016 at 11:49 AM, Michael Moore <michaeljmoore@gmail.com> wrote:
I want to UPDATE a table but I will not know until run time which columns will be updated. I would like to do something like this:
update mytest t (SELECT * FROM json_populate_record(null::mytest,'{"key":22,"header":44,"ident":66,"static01":"this","static02":"that"}'));
 
In other words, I will be receiving a json document on an input parameter. The columns named in the json document can be a sub set of those that exist in the table. 


If a key is defined but has a value of null what do you want to do?

​not fully tested...​

UPDATE mytest
SET col1 = COALESCE(src.col1, mytest.col1)
​FROM (SELECT * FROM json_populate_record(...)) src;​

json_populate_record outputs the full structure of mytest regardless of its input so simply match every column and use COALESCE to choose between the value in the json and the value already on the table.

Aside from the null json value issue that should work - if not you probably need to describe your problem in more detail.

David J.

В списке pgsql-sql по дате отправления:

Предыдущее
От: Hector Vass
Дата:
Сообщение: Re: update without SET ?
Следующее
От: Michael Moore
Дата:
Сообщение: Re: update without SET ?