1

As an instant example:

A function that returns a table:

=> create or replace function testf(x integer)
     returns table(a integer, b integer)
   language sql
   as $function$
       select * from (values(1,2)) as foo(a,b)
   $function$;

Calling it so it returns a record (or whatever you call it):

=> select testf(3);
 testf
-------
 (1,2)

Calling it so it returns a table (good):

=> select * from  testf(3);
 a | b
---+---
 1 | 2

But how do I call it so the parameters come from a query?

=> select s.n, testf(s.n) from (select 3 as n union select 4) s;
 n | testf
---+-------
 3 | (1,2)
 4 | (1,2)            <-- not a table (or row with column names)


=> select * from  testf(s.n) from (select 3 as n) s;
 ERROR:  syntax error at or near "from"
 LINE 1: select * from  testf(s.n) from (select 3 as n) s;

1 Answer 1

2

Use a LATERAL subquery:

SELECT *
FROM  (VALUES (3), (4)) AS s(n)
     , testf(s.n);

The comma is short notation for CROSS JOIN LATERAL, since LATERAL is assumed automatically with set-returning functions in the FROM clause.

Related:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.