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.
genid()
, store it in a variable and then use it in your update statement? – Repertorygenid
+ a row-level trigger that updates a, b and c on insert/update of that column an option? – Slate