Re: PLpgSQL FOR IN EXECUTE question - Mailing list pgsql-sql

From Josh Berkus
Subject Re: PLpgSQL FOR IN EXECUTE question
Date
Msg-id web-1816701@davinci.ethosmedia.com
Whole thread Raw
In response to PLpgSQL FOR IN EXECUTE question  (Christoph Haller <ch@rodos.fzk.de>)
List 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;



pgsql-sql by date:

Previous
From: Jean-Luc Lachance
Date:
Subject: Re: Quartile (etc) ranking in a SQL statement?
Next
From:
Date:
Subject: Re: query optimization question