Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 10A709FBE17 for ; Tue, 19 Jun 2007 15:05:20 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 50437-07 for ; Tue, 19 Jun 2007 15:05:14 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 63AD69FB771 for ; Tue, 19 Jun 2007 15:05:16 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from elasmtp-mealy.atl.sa.earthlink.net (elasmtp-mealy.atl.sa.earthlink.net [209.86.89.69]) by svr4.postgresql.org (Postfix) with ESMTP id 007D95AF2CB for ; Tue, 19 Jun 2007 18:05:08 +0000 (GMT) Received: from [69.86.105.158] (helo=[192.168.0.102]) by elasmtp-mealy.atl.sa.earthlink.net with asmtp (Exim 4.34) id 1I0i4h-0007BV-Gc for pgsql-sql@postgresql.org; Tue, 19 Jun 2007 14:05:07 -0400 Message-ID: <46781ACA.1090404@bucksvsbytes.com> Date: Tue, 19 Jun 2007 14:04:58 -0400 From: John Gunther User-Agent: Thunderbird 1.5.0.12 (Windows/20070509) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: joining a table whose name is stored in the primary record References: <4674F357.5040409@bucksvsbytes.com> <20070617120646.GA29549@KanotixBox> In-Reply-To: <20070617120646.GA29549@KanotixBox> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-ELNK-Trace: 012f15a0baae39ad94f5150ab1c16ac0c864d17e3813b99a023bfbdcf28b8a61e5543d6401aa47a0350badd9bab72f9c350badd9bab72f9c350badd9bab72f9c X-Originating-IP: 69.86.105.158 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.002 tagged_above=0 required=5 tests=AWL=0.001, BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 200706/144 X-Sequence-Number: 28634 Andreas Kretschmer wrote: > create or replace function zip_foo(OUT out_id int, OUT out_name text, OUT out_name2 text) returns setof record as $$ > declare > my_rec RECORD; > my_name TEXT; > begin > for my_rec in select id, name, parent_tbl, parent_id from zip LOOP > execute 'select name from ' || my_rec.parent_tbl || ' where id = ' || my_rec.parent_id || ';' into my_name; > out_id := my_rec.id; > out_name := my_rec.name; > out_name2 := my_name; > return next; > end loop; > end; > $$ language plpgsql; > Thanks, Andrew. I was hoping for a pure SQL solution but your idea will certainly work. John