Skip site navigation (1) Skip section navigation (2)

Function returning setof taking parameters from another table

From: Marcin Stępnicki <mstepnicki(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Function returning setof taking parameters from another table
Date: 2008-07-31 13:03:25
Message-ID: 179149fe0807310603u2be9f793rfba31cfd7d92032e@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-sql
Hello.

I've got a function which returns set of records:

select * from f_test(123);

param |  val1 |  val2
---------------------------
  123   |   1  |  17
  123   |   2   |  18



I'd like to execute it multiple times with parameters from other
query, like (it doesn't work of course):

select * from f_test(x.id)
join x on (x.id in (123,124,125));

in order to get:

param |  val1 |  val2
---------------------------
  123   |   1   |  17
  123   |   2   |  18
  124   |   4   |  179
  125   |   13 |  18
  125   |   15 |  12
  125   |   14 |  112

So far the only method I can think of is to use union all with
different parametrs, like:

select * from f_test(123)
union all
select * from f_test(124)
union all
select * from f_test(125);

But it is not flexible, I'd like to have parameters stored in another table.

I can also write a pl/pg function, create a string like this with
unions and EXECUTE it. However, it seems ugly to me.

Is there another way?

Regards,
Marcin

Responses

pgsql-sql by date

Next:From: Tom LaneDate: 2008-07-31 22:51:32
Subject: Re: Problem with ORDER BY and DISTINCT ON
Previous:From: Richard HuxtonDate: 2008-07-30 18:33:42
Subject: Re: Identifying which column matches a full text search

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group