Unix Technical Forum

Returning setof records

This is a discussion on Returning setof records within the pgsql Admins forums, part of the PostgreSQL category; --> Hello, I'm trying to return multiple records from a function. I'm having a little trouble getting past the "ERROR: ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Admins

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-10-2008, 06:44 AM
Scott Schulthess
 
Posts: n/a
Default Returning setof records

Hello,



I'm trying to return multiple records from a function. I'm having a
little trouble getting past the "ERROR: wrong record type supplied in
RETURN NEXT

CONTEXT: PL/pgSQL function "placenamesearch" line 5 at return next"
I've had this error before and fixed it by defining columns analogous to
the returned columns to output the records into. However I can't fix it
this time. Thanks for your help ahead of time!



<code>

create function placenamesearch(place text, state integer, county text,
place text, match text) returns setof record as '

declare myview record;

begin

FOR myview IN SELECT featurename, countyname, state, featuretype,
elevation, cellname, primarylatdd, primarylondd from gnis_placenames
where featurename like place and statefips=state and countyname=county
limit 200

LOOP

return next myview;

END LOOP;

return;

end; '

language 'plpgsql';

</code>



Here's my select statement



select * from placenamesearch('Cheshire',9,'New Haven','text', 'text')
as (FeatureName varchar, CountyName varchar, State varchar, featuretype
varchar, Elevation int4,CellName varchar, PrimaryLatDD float8,
PrimaryLonDD float8);



Now here's my table definition



state char(2),

featurename varchar(96),

featuretype varchar(24),

countyname varchar(64),

statefips int4,

countyfips int4,

primarylatdd float8,

primarylondd float8,

elevation int4 DEFAULT -1,

cellname varchar(32),



So I was thinking that I was just using the shortened notation of the
column data types wrong. I tried writing them out 'in full' aka
varchar(64), etc but that didn't work either. THANKS!



-Scott



Scott Schulthess




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-10-2008, 06:44 AM
Tom Lane
 
Posts: n/a
Default Re: Returning setof records

"Scott Schulthess" <scott@topozone.com> writes:
> select * from placenamesearch('Cheshire',9,'New Haven','text', 'text')
> as (FeatureName varchar, CountyName varchar, State varchar, featuretype
> varchar, Elevation int4,CellName varchar, PrimaryLatDD float8,
> PrimaryLonDD float8);

....
> state char(2),


You've got the State output column declared as varchar, but actually
it's char. PG will often do automatic conversion from one type to the
other, but in this context it's picky...

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:08 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com