dynamic plpgsql command on a record type

From: Willy-Bas Loos <willybas(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: dynamic plpgsql command on a record type
Date: 2010-02-09 11:07:02
Message-ID: 1dd6057e1002090307j6c770aa6ued5a02bc84e258ed@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

in my plpgsql function I'm looping through tables and their fields and i
want to return their field names and the corresponding values.
It all works fine, except i can't retrieve the values, which is really a
bummer.
I tried a couple of things (in vain), of which this seemed the most
promising:

<<records>>
FOR t_record IN EXECUTE
t_qstring
LOOP
<<fields>>
FOR t_i IN 1..array_upper(t_fields, 1)
LOOP
t_rec_out.field_name := t_fields[t_i];
--retrieve the value of this column, this record, this table into
"field_value" for return.
t_qstring := 't_record.'||t_fields[t_i]; --re-using t_qstring!
EXECUTE t_qstring INTO t_rec_out.field_value; --<==== happens here ===
RETURN NEXT t_rec_out;

On EXECUTE, i get the error:
ERROR: syntax error at or near "t_record"

I also tried:

<<records>>
FOR t_record IN EXECUTE
t_qstring
LOOP
<<fields>>
FOR t_i IN 1..array_upper(t_fields, 1)
LOOP
t_rec_out.field_name := t_fields[t_i];
--retrieve the value of this column, this record, this table into
"field_value" for return.
t_rec_out.field_value := t_record.t_fields[t_i]; --<==== happens here
===
RETURN NEXT t_rec_out;

And then i get the error:
ERROR: record "t_record" has no field "t_fields"

Any tips there?

Cheers,

WBL

--
"Patriotism is the conviction that your country is superior to all others
because you were born in it." -- George Bernard Shaw

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ben Campbell 2010-02-09 11:25:33 problems maintaining boolean columns in a large table
Previous Message Scott Marlowe 2010-02-09 10:38:40 Re: Memory Usage and OpenBSD