Re: Update multiple rows in a table with different values

From: shankha <shankhabanerjee(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Update multiple rows in a table with different values
Date: 2016-07-01 19:03:53
Message-ID: CAO_L6qGNM5k=-a35ROUA0bxVuLfq9XMNNep=vKk5+XzsEC2GNg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi Tom,
Thanks for your suggestion.

I got it working:

CREATE OR REPLACE FUNCTION s.updatefunc1(BigInt[], BigInt[])
RETURNS void as $$
BEGIN
FOR i IN array_lower($1, 1) .. array_upper($1, 1)
LOOP
update s.t1
SET c3 = $2[i]
WHERE c2 = $1[i];
END LOOP;
END;
$$
LANGUAGE plpgsql;

Is there a better way to do it using : unnest.

Thanks
Shankha Banerjee

On Fri, Jul 1, 2016 at 10:59 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> shankha <shankhabanerjee(at)gmail(dot)com> writes:
>> PREPARE updatearrayplan(BigInt[], BigInt[]) AS
>> for i in size($1)
>> DO
>> update s.t1
>> SET c3 = $2[$i]
>> WHERE c2 = $1[$i]
>> END FOR
>
>> In this prepared statement I am just trying to explain the algorithm.
>> I do not know the exact syntax.
>
> You would need to write a plpgsql function in order to have a loop like
> that; there's no loops in bare SQL.
>
> regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Merlin Moncure 2016-07-01 19:41:49 Re: PSQL does not remove obvious useless joins
Previous Message Sfiligoi, Igor 2016-07-01 17:17:55 PSQL does not remove obvious useless joins