19 messages in org.postgresql.pgsql-generalRe: dynamic field names in a function.
FromSent OnAttachments
Soma InterestingMar 29, 2001 2:38 pm 
Jeff EckermannMar 30, 2001 9:29 am 
Soma InterestingMar 30, 2001 10:06 am 
Soma InterestingMar 30, 2001 10:57 am 
Soma InterestingMar 30, 2001 1:58 pm 
Jeff EckermannMar 30, 2001 2:09 pm 
Soma InterestingMar 30, 2001 2:33 pm 
will trillichMar 30, 2001 3:46 pm 
Soma InterestingMar 30, 2001 4:38 pm 
will trillichMar 30, 2001 4:47 pm 
Soma InterestingMar 30, 2001 6:02 pm 
Eric G. MillerMar 30, 2001 7:29 pm 
Tom LaneMar 30, 2001 9:42 pm 
will trillichMar 30, 2001 10:04 pm 
will trillichMar 30, 2001 10:13 pm 
Tom LaneMar 31, 2001 4:57 pm 
will trillichApr 1, 2001 9:23 pm 
Tom LaneApr 1, 2001 11:27 pm 
Soma InterestingApr 3, 2001 1:23 pm 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: dynamic field names in a function.Actions...
From:Eric G. Miller (eg@jps.net)
Date:Mar 30, 2001 7:29:34 pm
List:org.postgresql.pgsql-general

On Thu, Mar 29, 2001 at 02:38:31PM -0800, Soma Interesting wrote:

I want to be able to reference NEW.field_0 though NEW.field_x where x is coming from NEW.qty in a FOR loop of pl/pgsql function. Is this possible?

In other words:

FOR j IN 0..NEW.str LOOP

ans := ''q'' || i || ''a'' || j; cor := ''q'' || i || ''c'' || j; eval := 'q'' || i || ''e'' || j;

IF NEW.ans = NEW.cor THEN NEW.eval := 1; END IF;

END LOOP;

I think maybe querying system catalogs might help your approach. I'm not entirely clear on what you're trying to do, but you can get the name of the relation that caused the trigger to fire (TG_RELNAME). Then query the pg_class table for the "oid" of the class where relnam = TG_RELNAME, join with pg_attribute on pg_class.oid = pg_attribute.attrelid and pg_attribute.attnum > 0 (to skip internal system fields). Then you have a set of records containing all of the field names for the relation which you can compare to the concatenation of your "field" and NEW.qty. Hope this is making some sense. Here's a quick example query on a known relation called "units".

select pg_attribute.* from pg_attribute, pg_class where pg_attribute.attrelid = pg_class.oid and pg_class.relname = 'units' and pg_attribute.attnum > 0;