I'm not sure how to achieve something like the following:
CREATE OR REPLACE FUNCTION fnJobQueueBEFORE() RETURNS trigger AS $$ DECLARE shadowname varchar := TG_TABLE_NAME || 'shadow'; BEGIN INSERT INTO shadowname VALUES(OLD.*); RETURN OLD; END; $$ LANGUAGE plpgsql;
those. Insert values โโinto a table with a dynamically generated name.
Executing the above code gives:
ERROR: relation "shadowname" does not exist LINE 1: INSERT INTO shadowname VALUES(OLD.*)
Variables do not seem to expand / resolve, like table names. I did not find a link to this in the Postgres manual.
I have already experimented with EXECUTE as follows:
EXECUTE 'INSERT INTO ' || quote_ident(shadowname) || ' VALUES ' || OLD.*;
But no luck:
ERROR: syntax error at or near "," LINE 1: INSERT INTO personenshadow VALUES (1,sven,,,)
The RECORD type seems to be lost: OLD.* Seems to be converted to a string and gets repaired, which leads to different types of problems (for example, NULL values).
Any ideas?
plpgsql triggers postgresql dynamic-sql
sschober Oct 27 '11 at 9:57 2011-10-27 09:57
source share