Unix Technical Forum

Create a function that returns multiple columns

This is a discussion on Create a function that returns multiple columns within the Pgsql General forums, part of the PostgreSQL category; --> Hello, I want to write a pl/pgsql function that returns one or more rows with multiple (1..n) columns. The ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-10-2008, 12:19 AM
marc.hanisch@googlemail.com
 
Posts: n/a
Default Create a function that returns multiple columns

Hello,

I want to write a pl/pgsql function that returns one or more rows with
multiple (1..n) columns. The tricky thing about this is, that I don't
know how many columns the return type will have.

The reason for that is, that I have two tables:

table1
id type
-- ----
01 employee
02 student

table2
id refid name
-- ----- -----
01 01 John
02 01 Max

When I select from table1 for all employees, I want a result like
this:

id type name1 name2
-- ---- ----- -----
01 employee John Max

So my thoughts where to set up a function which creates the type in
dependence of the results from table2 and then to set up another
function with the created return type:

CREATE TYPE testtype as (id int, type character varying, name1
character varying, name1 character varying);

CREATE OR REPLACE FUNCTION test() RETURNS SETOF testtype as $$
....
$$ LANGUAGE plpgsql;

But when I try this, psql tells me that the type is unknown...
Any ideas?! Thank you in advance!

Marc
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-10-2008, 12:19 AM
SunWuKung
 
Posts: n/a
Default Re: Create a function that returns multiple columns

On jan. 25, 16:11, "marc.hani...@googlemail.com"
<marc.hani...@googlemail.com> wrote:
> Hello,
>
> I want to write a pl/pgsql function that returns one or more rows with
> multiple (1..n) columns. The tricky thing about this is, that I don't
> know how many columns the return type will have.
>
> The reason for that is, that I have two tables:
>
> table1
> id type
> -- ----
> 01 employee
> 02 student
>
> table2
> id refid name
> -- ----- -----
> 01 01 John
> 02 01 Max
>
> When I select from table1 for all employees, I want a result like
> this:
>
> id type name1 name2
> -- ---- ----- -----
> 01 employee John Max
>
> So my thoughts where to set up a function which creates the type in
> dependence of the results from table2 and then to set up another
> function with the created return type:
>
> CREATE TYPE testtype as (id int, type character varying, name1
> character varying, name1 character varying);
>
> CREATE OR REPLACE FUNCTION test() RETURNS SETOF testtype as $$
> ...
> $$ LANGUAGE plpgsql;
>
> But when I try this, psql tells me that the type is unknown...
> Any ideas?! Thank you in advance!
>
> Marc


I think what you are looking for is a crosstab.
If this is a generic problem than as far as I know there is no way you
could do this in Postgres (MSAccess could do it) - if you don't know
the resulting columns.
At least I have been looking for a crosstab solution in Postgres where
you didnt have to specify the resulting colums for a long-long time
and couldnt find it.

If you have a special case where you know the possible resulting
columns you could it by either CASE or using tablefunc crosstab - just
specify all possible output columns.

Hth.
SWK.
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 11:17 AM.


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