Re: Update multiple rows in a table with different values

Поиск
Список
Период
Сортировка
От David G. Johnston
Тема Re: Update multiple rows in a table with different values
Дата
Msg-id CAKFQuwYrcMA=M=NeXz6TeMWu0-Ek6-aM0vxhh9_4OJFszzntow@mail.gmail.com
обсуждение исходный текст
Ответ на Update multiple rows in a table with different values  (shankha <shankhabanerjee@gmail.com>)
Список pgsql-general
On Fri, Jul 1, 2016 at 10:26 AM, shankha <shankhabanerjee@gmail.com> wrote:

    PREPARE updatearrayplan(BigInt[], BigInt[]) AS
        for i in size($1)
        DO
            update s.t1
            SET c3 = $2[$i]
            WHERE c2 = $1[$i]
        END FOR

    EXECUTE updatearrayplan({20, 30}, {275, 375})

After execution of updatearrayplan I am expecting the rows to have
these values  20 -> 275 , 30 -> 375


Have you looked at CREATE FUNCTION​?

I'd suggest the plpgsql language.
 
Is there a way to update multiple rows with different column values
passed in as array.

​No.  All rows identified by a single where clause are updated using the same expression.  Though I suppose you could try something like:

c3 = CASE WHEN c2= 20 THEN 275 WHEN c2= 30 THEN 375​ END
WHERE c2IN (20, 30)
 
Also is there a guarantee that the order of the
arrays will be maintained.

That question is too broad.  Direct iteration of an array will be done in order.  Whether, post-iteration, the resultant records remain in order is not promised.

David J.

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

Предыдущее
От: Adrian Klaver
Дата:
Сообщение: Re: Update multiple rows in a table with different values
Следующее
От: shankha
Дата:
Сообщение: Re: Update multiple rows in a table with different values