From: Adrian Klaver <adrian.klaver@gmail.com>
To: a.visinoni@autron.it
Cc: pgsql-sql@postgresql.org
Subject: Re: plpgsql loop question
Date: Wed, 10 Feb 2010 12:56:34 -0800
Message-ID: <4B731D82.2010202@gmail.com> (raw)
In-Reply-To: <4B72DEDE.3070507@autron.it>
References: <4B72DEDE.3070507@autron.it>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: adrian.klaver@gmail.com, a.visinoni@autron.it
Subject: Re: plpgsql loop question
In-Reply-To: <4B731D82.2010202@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox