Re: Possible to access value in RECORD without knowing column name?

From: Richard Albright <ralbright(at)insiderscore(dot)com>
To: Collin Peters <cadiolis(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Possible to access value in RECORD without knowing column name?
Date: 2007-05-02 19:26:23
Message-ID: 1178133983.26840.5.camel@MSI1036
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

my_column = 'foo'
sql = 'select col1, col2, ' || my_column || ' as bar from mytable;
for myrecord in execute sql
loop
myvariable = myrecord.bar
end loop;

On Wed, 2007-05-02 at 12:17 -0700, Collin Peters wrote:
> In plpgsl, if I have a RECORD variable that is populated via some
> dynamic SQL, is it possible to access the columns in that RECORD
> object without knowing the column names? I.e. Can I grab whatever
> value is the 3rd column?
>
> random_colname = 'foobar';
> sql = 'SELECT col1, col2, ' || random_colname || ' FROM table';
>
> FOR mviews IN EXECUTE sql LOOP
> --possible to access 3rd column of mviews? something like mviews[3]?
> END LOOP;
>
> Regards,
> Collin
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly
--
Rick Albright
Senior Quantitative Analyst
Indie Research, LLC
254 Witherspoon Street
Princeton, NJ 08542
(609)497-1030
ralbright(at)insiderscore(dot)com

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Broersma Jr 2007-05-03 00:17:07 Joe Celko's method to "generate_series" not working?
Previous Message Collin Peters 2007-05-02 19:17:08 Possible to access value in RECORD without knowing column name?