Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id E9858632CF0 for ; Wed, 10 Feb 2010 16:56:49 -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 38967-01 for ; Wed, 10 Feb 2010 20:56:34 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-pz0-f179.google.com (mail-pz0-f179.google.com [209.85.222.179]) by mail.postgresql.org (Postfix) with ESMTP id B766E63265C for ; Wed, 10 Feb 2010 16:56:38 -0400 (AST) Received: by pzk9 with SMTP id 9so464499pzk.28 for ; Wed, 10 Feb 2010 12:56:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:cc:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=F5yv5ilrpzT+c8CsdWurIsQ+HUKFeeip8S6bD5hHQME=; b=DfWObJGmeSRh6mDIiNh7Pu2r16e7ze+TgRUfX3uzEKc+NcbjxTzE9pjr9/9mP/xSTu z/acriOBiWOA4VwRBTFza+fVEgfu8EdEKSglUKPutxzVHM4viBFxAukGcGfbExL3Ff7K j4jYViy0uWmoeZfRKuMS83oVXv3a44q8IFpBw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; b=PBKa6/UF9HggWX8JOltddrtLtcpDHz4H2WGN9cY6RveIvv2PpuwRffmQ8tl777iJZ5 7VkNhvP/flRvxl2WGx6jTYJzJ4CuFaK+XGnLtUQF0ywxwoXHx3nTao7LxMckNW92PR6f jYa46I4GGjIBWcTalWC0nZkcK6lHOxm7Pb2RM= Received: by 10.141.101.1 with SMTP id d1mr486277rvm.219.1265835397154; Wed, 10 Feb 2010 12:56:37 -0800 (PST) Received: from ?192.168.0.114? (c-24-17-179-185.hsd1.wa.comcast.net [24.17.179.185]) by mx.google.com with ESMTPS id 23sm1866510pzk.8.2010.02.10.12.56.35 (version=SSLv3 cipher=RC4-MD5); Wed, 10 Feb 2010 12:56:36 -0800 (PST) Message-ID: <4B731D82.2010202@gmail.com> Date: Wed, 10 Feb 2010 12:56:34 -0800 From: Adrian Klaver User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.7) Gecko/20100111 Thunderbird/3.0.1 MIME-Version: 1.0 To: a.visinoni@autron.it CC: pgsql-sql@postgresql.org Subject: Re: plpgsql loop question References: <4B72DEDE.3070507@autron.it> In-Reply-To: <4B72DEDE.3070507@autron.it> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.164 tagged_above=-10 required=5 tests=AWL=0.435, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201002/41 X-Sequence-Number: 33640 On 02/10/2010 08:29 AM, Andrea Visinoni wrote: > 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 > One thing I would do is rename your zones record variable. pgsql does not deal well with a variable having the same name as a schema object, in this case your table zones. -- Adrian Klaver adrian.klaver@gmail.com