plpgsql loop question
plpgsql loop question
От:
Andrea Visinoni <a.visinoni@autron.it>
Дата:
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 loopfor 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
Re: plpgsql loop question
От:
Justin Graf <justin@magwerks.com>
Дата:
On 2/10/2010 11: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 > Pg will not auto build the columns outputted from generic type record. The function needs to describe what the output is going to look like. The function can inherit the layout from a table or create the new pg data type but PG does not know what the data looks like to create the result set. . All legitimate Magwerks Corporation quotations are sent in a .PDF file attachment with a unique ID number generated by our proprietary quotation system. Quotations received via any other form of communication will not be honored. CONFIDENTIALITY NOTICE: This e-mail, including attachments, may contain legally privileged, confidential or other information proprietary to Magwerks Corporation and is intended solely for the use of the individual to whom it addresses. If the reader of this e-mail is not the intended recipient or authorized agent, the reader is hereby notified that any unauthorized viewing, dissemination, distribution or copying of this e-mail is strictly prohibited. If you have received this e-mail in error, please notify the sender by replying to this message and destroy all occurrences of this e-mail immediately. Thank you.
Re: plpgsql loop question
От:
Adrian Klaver <adrian.klaver@gmail.com>
Дата:
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