Search Postgresql Archives

Re: Update multiple rows in a table with different values

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Jul 1, 2016 at 10:26 AM, shankha <shankhabanerjee@xxxxxxxxx> 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.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux