X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id DF714D8C36 for ; Mon, 12 Sep 2005 20:03:35 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 35909-10 for ; Mon, 12 Sep 2005 23:03:29 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id 5219BD796A for ; Mon, 12 Sep 2005 20:03:30 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j8CN3PSp043554 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 12 Sep 2005 17:03:28 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.3/8.13.3) with ESMTP id j8CN3OvK063581; Mon, 12 Sep 2005 17:03:24 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.3/8.13.3/Submit) id j8CN3Nmk063580; Mon, 12 Sep 2005 17:03:23 -0600 (MDT) (envelope-from mfuhr) Date: Mon, 12 Sep 2005 17:03:23 -0600 From: Michael Fuhr To: gherzig@fmed.uba.ar Cc: pgsql-sql@postgresql.org Subject: Re: refer a column as a varible name? Message-ID: <20050912230323.GA63469@winnie.fuhr.org> References: <56093.157.92.152.53.1126538482.squirrel@webmail.fmed.uba.ar> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <56093.157.92.152.53.1126538482.squirrel@webmail.fmed.uba.ar> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.005 required=5 tests=[AWL=0.005] X-Spam-Level: X-Archive-Number: 200509/133 X-Sequence-Number: 22680 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