How can I address a column from a record in a plpgsql function dynamically?
In the following snippet I have access to a variable entity.colname that contains the column that should be checked in the IF constrol structure. I'm looking into how I can replace the foobar part in the snippet below with entity.colname. Is that possible in plpgsql?
IF NEW.foobar IS NULL THEN
RAISE EXCEPTION '% cannot be null', foobar;
END IF;
This is something that could but doesn't work.
IF NEW.entity.colname IS NULL THEN
RAISE EXCEPTION '% cannot be null', entity.colname;
END IF;
PS: the example above is just for illustration what I'm looking to achieve, don't judge the functionality :)
PS2: I'm using postgresql 9.1
NEW.entity.colnamerather than justNEW.colname? – Jack Douglas♦ Jul 25 '12 at 14:04entity.colnameis supposed to hold the name of the column. The OP needs dynamic SQL. – Erwin Brandstetter Jul 25 '12 at 14:12