jeudi 1 janvier 2015

How to write a PL/pgSQL function to execute a dynamic query?


I am writing a function that needs to execute an UPDATE on every single row for a query. But it's not working. It doesn't loop over the rows at all. Can you help me spot my error? Here is what my function looks like:



CREATE OR REPLACE FUNCTION cs_parcel_postproc(cid int, props text) RETURNS integer AS $$
DECLARE
cid int;
props text;
BEGIN
RAISE NOTICE 'Postprocessing parcels...';

EXECUTE 'UPDATE parcels SET ' || props || ' WHERE city_id =' || cid || ';';
RAISE NOTICE 'Done post processing parcels.';
RETURN 1;
END;
$$ LANGUAGE plpgsql;


EDIT: I discovered that cid is NULL inside the function. I don't understand how that could be.





Aucun commentaire:

Enregistrer un commentaire