Re: refer a column as a varible name?

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: gherzig(at)fmed(dot)uba(dot)ar
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: refer a column as a varible name?
Date: 2005-09-12 23:03:23
Message-ID: 20050912230323.GA63469@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Mon, Sep 12, 2005 at 12:21:22PM -0300, gherzig(at)fmed(dot)uba(dot)ar wrote:
> suppose the
>
> type mycolumn as (field1, varchar, field2 varchar)
> and
>
> field_name = ''field1''
>
> and returnValue declared as mycolumn
> ...
> can i say returnValue.$field_name = ''ok''?

To achieve this in PL/pgSQL you'll need to use a conditional statement
(IF field_name = 'field1' THEN ...). I'm not sure if a solution
involving EXECUTE is possible; if so then it's probably non-obvious.

What version of PostgreSQL are you using, and do you have a requirement
to use PL/pgSQL? In 8.0 PL/Perl can return composite types and such
an assignment would be trivial:

CREATE TYPE mycolumn AS (field1 varchar, field2 varchar);

CREATE FUNCTION foo(varchar) RETURNS mycolumn AS $$
my $field_name = $_[0];
my $returnValue = {$field_name => "ok"};
return $returnValue;
$$ LANGUAGE plperl IMMUTABLE STRICT;

SELECT * FROM foo('field1');
field1 | field2
--------+--------
ok |
(1 row)

SELECT * FROM foo('field2');
field1 | field2
--------+--------
| ok
(1 row)

--
Michael Fuhr

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Greg Sabino Mullane 2005-09-13 00:38:49 Re: Need help with `unique parents` constraint
Previous Message Leif B. Kristensen 2005-09-12 18:02:34 Re: Need help with 'unique parents' constraint