X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 399A6329D7C for ; Sun, 26 Sep 2004 19:33:58 +0100 (BST) 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 60009-06 for ; Sun, 26 Sep 2004 18:33:53 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id 276E9329D58 for ; Sun, 26 Sep 2004 19:33:52 +0100 (BST) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.1/8.13.1) with ESMTP id i8QIXk8M055047 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sun, 26 Sep 2004 12:33:49 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.1/8.13.1) with ESMTP id i8QIXkFl044732; Sun, 26 Sep 2004 12:33:46 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.1/8.13.1/Submit) id i8QIXhi1044731; Sun, 26 Sep 2004 12:33:43 -0600 (MDT) (envelope-from mfuhr) Date: Sun, 26 Sep 2004 12:33:43 -0600 From: Michael Fuhr To: Sz?lka Tam?s Cc: pgsql-sql@postgresql.org Subject: Re: plpgsql - accessing fields of record type Message-ID: <20040926183343.GA64575@winnie.fuhr.org> References: <4156EB3D.3070805@freestart.hu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4156EB3D.3070805@freestart.hu> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/221 X-Sequence-Number: 19067 On Sun, Sep 26, 2004 at 06:15:57PM +0200, Sz?lka Tam?s wrote: > I have a table with field named "XY_1", "XY_2", ... "XY_255". > I want to access the values in the table from a plpgsql function somehow > in a loop with the iterative variable. Can I address the appropriate > ''XY_'' || loopvariable field with the help of the loop variable? Could you tell us a little more about the application? I'm wondering if the table layout is really the best way to organize the data. > I have thought of an alternative solution: can plpgsql convert a record > type into array? (Then I could address the field value with the nth > element of the array) You could use an array constructor if none of the fields could possibly be NULL (elements of an array can't be NULL). The code would look something like this: FOR r IN SELECT ARRAY[XY_1, XY_2, ... XY_255] AS a FROM tbl LOOP FOR i IN 1 .. 255 LOOP -- do something with r.a[i] END LOOP; END LOOP; If you didn't want to type all the field names, you could construct the query string with a loop and use FOR-IN-EXECUTE, but then you'd lose the advantage of a prepared execution plan on subsequent calls to the function, not to mention the time spent constructing the query string on each call. As I suggested above, perhaps there's a better way to organize the data. -- Michael Fuhr http://www.fuhr.org/~mfuhr/