Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id DF8376328D8 for ; Wed, 10 Feb 2010 12:35:19 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 90749-05 for ; Wed, 10 Feb 2010 16:35:08 +0000 (UTC) X-Greylist: delayed 00:06:14.280052 by SQLgrey-1.7.6 Received: from MTA005E.interbusiness.it (MTA005E.interbusiness.it [88.44.62.5]) by mail.postgresql.org (Postfix) with ESMTP id 0D08B6325BF for ; Wed, 10 Feb 2010 12:35:08 -0400 (AST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqkFAOdscktYPlfaPGdsb2JhbACaaQEBAQEYTbwEhFUE Received: from host218-87-static.62-88-b.business.telecomitalia.it (HELO localhost) ([88.62.87.218]) by ibs10.interbusiness.it with ESMTP; 10 Feb 2010 17:28:51 +0100 Received: from [10.0.2.12] by localhost with esmtp (Exim 4.63) (envelope-from ) id 1NfFQl-00013i-Rk for pgsql-sql@postgresql.org; Wed, 10 Feb 2010 17:28:48 +0100 Message-ID: <4B72DEDE.3070507@autron.it> Date: Wed, 10 Feb 2010 17:29:18 +0100 From: Andrea Visinoni Reply-To: a.visinoni@autron.it User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.9.1.7) Gecko/20100111 Lightning/1.0b1 Thunderbird/3.0.1 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-SA-Exim-Connect-IP: 10.0.2.12 X-SA-Exim-Mail-From: a.visinoni@autron.it Subject: plpgsql loop question X-SA-Exim-Version: 4.2.1 (built Tue, 09 Jan 2007 17:23:22 +0000) X-SA-Exim-Scanned: Yes (on localhost) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=-10 required=5 tests=BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 201002/37 X-Sequence-Number: 33636 hi, i have a table called "zones": idzone, zone_name and several tables called zonename_records (same structure), where zonename is one of the zone_name in the "zones" table. What i want to do is a function that union all of this tables dinamically based on "zones" table, this is what i've done so far: CREATE OR REPLACE FUNCTION get_all_records() RETURNS SETOF record AS $BODY$DECLARE zones record; recs record; BEGIN for zones in select lower(zone_name) as n from zones loop for recs in select * from quote_ident(zones.n || '_records') loop return next recs; end loop; end loop; RETURN; END; $BODY$ LANGUAGE 'plpgsql' VOLATILE COST 100; but i get this error! ERROR: wrong record type supplied in RETURN NEXT CONTEXT: PL/pgSQL function "get_all_records" line 9 at RETURN NEXT Andrea