Any idea what I'm doing wrong here?
CREATE OR REPLACE FUNCTION update_prices_in_ord1( ) RETURNS void AS $$
DECLARE
cur CURSOR for select ord2.ord1_id, sum(ord2.price*ord2.qty) as totprice from ord2 group by ord1_id;
--tmpid int;
--tmpid ord2.ord1_id%TYPE;
--tmpprice float;
mydata RECORD;
BEGIN
open cur;
loop
--fetch cur into tmpid,tmpprice;
fetch cur into mydata;
--raise notice 'price=%, id=%', tmpprice, tmpid;
raise notice 'price=%, id=%', mydata.totprice, mydata.ord1_id;
update ord1 set price=mydata.totprice where id=mydata.ord1_id;
end loop;
close cur;
END;
$$ LANGUAGE plpgsql;
As you can see I've tried a few options (in comments) but no luck. All I get is infinite nulls:
NOTICE: price=<NULL>, id=<NULL>
If I run the sql of cursor alone it runs fine:
testdb=# select ord2.ord1_id, sum(ord2.price*ord2.qty) as totprice from ord2 group by ord1_id;
ord1_id | totprice
---------+----------
14 | 10
27 | 42.5
17 | 57.5
28 | 43
15 | 142
...
All I want to do is to update the ord1.price field, based on above totalprice, for the matching ord1.id.
Thanks a lot!