Unix Technical Forum

can plpgsql returns more flexibe value ?

This is a discussion on can plpgsql returns more flexibe value ? within the pgsql Hackers forums, part of the PostgreSQL category; --> Hello, pgsql-hackers I create a table, a type and a function like below: ------------------------------------ CREATE TABLE "User" ( "Id" ...


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

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-11-2008, 02:23 AM
Arnold.Zhu
 
Posts: n/a
Default can plpgsql returns more flexibe value ?

Hello, pgsql-hackers

I create a table, a type and a function like below:

------------------------------------
CREATE TABLE "User"
(
"Id" int4 NOT NULL DEFAULT nextval('public."User_Id_seq"'::text),
"Name" varchar(32) NOT NULL
);

------------------------------------
CREATE TYPE "UserSet" AS
(
"Id" int4,
"Name" varchar(32)
);

------------------------------------
CREATE OR REPLACE FUNCTION "UserSelectById"("@Id" int4)
RETURNS SETOF "User" AS
'
declare rec record;

begin

for rec in
select * from "User" where "Id" = "@Id"
loop
return next rec;
end loop;
return;

end; '
LANGUAGE 'plpgsql' VOLATILE;
------------------------------------

When I use
select * from "UserSelectById"(1);
it gives the right result set.


But when I change return type like this
------------------------------------
CREATE TYPE "UserSet" AS
(
"Id" int4,
"Name" varchar(32),
"LastLogin" timestamp --additional column
);

select * from "UserSelectById"(1) will give the following errors:

ERROR: wrong record type supplied in RETURN NEXT
CONTEXT: PL/pgSQL function "UserSelectById" line 8 at return next


This problem annoys me, if I have more than several hundred function
which returns different result set, like contains foreign keys,
I have to create many type for function's return.

Can plpgsql returns result set according to what exactly fetched,
then take return type as references to store data for return.

Whether there is some better way to deal with this problem?


Thanks & Regards

Arnold.Zhu
2005-01-21





---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-11-2008, 02:23 AM
Christopher Kings-Lynne
 
Posts: n/a
Default Re: can plpgsql returns more flexibe value ?

> This problem annoys me, if I have more than several hundred function
> which returns different result set, like contains foreign keys,
> I have to create many type for function's return.
>
> Can plpgsql returns result set according to what exactly fetched,
> then take return type as references to store data for return.
>
> Whether there is some better way to deal with this problem?


Yeah, you just make your function return 'SETOF record' and specify the
types when you do the select:

select * from func() as (a int, b text);

Chris

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

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 09:58 PM.


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