I have the following UPDATE statement:
update mytable
set a = first_part(genid()),
b = second_part(genid()),
c = third_path(genid())
where package_id = 10;
In this example the function genid()
is called three times for each row, which is wrong - I want it to be called only once for each row of mytable
.
I'm using PostgreSQL 8.4 database. How to write the correct update?
I've tried something like this:
update mytable
set a = first_part(g),
b = second_part(g),
c = third_path(g)
where package_id = 10
from genid() as g;
But it didn't work, because genid()
has been called only once for the whole update statement.
Have you tried Postgres' non-standard UPDATE .. FROM
clause? I imagine, this would work
update mytable
set a = first_part(gen.id),
b = second_part(gen.id),
c = third_path(gen.id)
from (
select genid() as genid, id
from mytable
where package_id = 10
) gen
where mytable.id = gen.id;
--and package_id = 10 -- This predicate is no longer necessary as the subquery
-- already filters on package_id, as Erwin mentioned
Note that I'm forcing genid()
to be called exactly once per record in mytable
within the subselect. Then I'm self-joining mytable
and gen
using a hypothetical id
column.
See the documentation here:
http://www.postgresql.org/docs/current/interactive/sql-update.html
This seems to have been introduced with Postgres 9.0 only, though. If that seems too complicated (i.e. not very readable), you can still resort to pgplsql as user Florin suggested here.
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