Re: PLpgSQL FOR IN EXECUTE question

From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: Christoph Haller <ch(at)rodos(dot)fzk(dot)de>, pgsql-sql(at)postgresql(dot)org
Subject: Re: PLpgSQL FOR IN EXECUTE question
Date: 2002-11-07 21:30:20
Message-ID: web-1816701@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Chris,

> FOR this_record IN
> EXECUTE ''SELECT ''
> || quote_ident($1)
> || ''FROM ''
> || quote_ident($2)
> LOOP
> list := list || '', '' || this_record.$1 ;
> END LOOP;
>
> As expected, accessing a field via this_record.$1
> does not work.
> Can it be done otherwise?

Yes. Alias the columns:

FOR this_record IN
EXECUTE ''SELECT ''
|| quote_ident($1)
|| '' AS col1 FROM ''
|| quote_ident($2)
|| '' AS col2'' LOOP
list := list || '', '' || this_record.col1 ;
END LOOP;

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Neil Conway 2002-11-07 21:56:35 Re: Cannot assign ROWTYPE, RECORD variables in PL/pgSQL
Previous Message Jean-Luc Lachance 2002-11-07 20:50:02 Re: Quartile (etc) ranking in a SQL statement?