pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
refer a column as a varible name?
3+ messages / 2 participants
[nested] [flat]

* refer a column as a varible name?
@ 2005-09-12 15:21  gherzig@fmed.uba.ar
  0 siblings, 1 reply; 3+ messages in thread

From: gherzig@fmed.uba.ar @ 2005-09-12 15:21 UTC (permalink / raw)
  To: pgsql-sql

Hi all. I have troubles trying to achieve this assignment:
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''?
There is a way to achieve this piece of code?

Thanks  a lot!

-- 
Gerardo Herzig
Direccion General de Organizacion y Sistemas
Facultad de Medicina
U.B.A.



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: refer a column as a varible name?
@ 2005-09-12 23:03  Michael Fuhr <mike@fuhr.org>
  parent: gherzig@fmed.uba.ar
  0 siblings, 1 reply; 3+ messages in thread

From: Michael Fuhr @ 2005-09-12 23:03 UTC (permalink / raw)
  To: gherzig@fmed.uba.ar; +Cc: pgsql-sql

On Mon, Sep 12, 2005 at 12:21:22PM -0300, gherzig@fmed.uba.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



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: refer a column as a varible name?
@ 2005-09-13 18:09  gherzig@fmed.uba.ar
  parent: Michael Fuhr <mike@fuhr.org>
  0 siblings, 0 replies; 3+ messages in thread

From: gherzig@fmed.uba.ar @ 2005-09-13 18:09 UTC (permalink / raw)
  To: Michael Fuhr <mike@fuhr.org>; +Cc: gherzig@fmed.uba.ar; pgsql-sql

Im using pg 8.0. I personally dont have problem in using pl/perl, but none
of my companions is a perl programmer, so i guess the IF..THEN seems to be
an easy (altough not preferable) solution.

Thank you very much Michael.

Gerardo
> On Mon, Sep 12, 2005 at 12:21:22PM -0300, gherzig@fmed.uba.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
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
>        subscribe-nomail command to majordomo@postgresql.org so that your
>        message can get through to the mailing list cleanly
>
>


-- 
Gerardo Herzig
Direccion General de Organizacion y Sistemas
Facultad de Medicina
U.B.A.



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2005-09-13 18:09 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-09-12 15:21 refer a column as a varible name? gherzig@fmed.uba.ar
2005-09-12 23:03 ` Michael Fuhr <mike@fuhr.org>
2005-09-13 18:09   ` gherzig@fmed.uba.ar

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox