Search code examples
sqlpostgresqlset-returning-functions

Postgresql calling a function in another function passing an argument from the main function


Iam using Postgresql and I have function which retrieves all the teachers(name, age and subject columns) which takes school id as an argument

current query looks like this

select id, (select * from getallteachers(schools.id)) 
from schools

this throws me an error saying subquery cant return multiple values, then I tried this

select id, getallteachers(schools.id)
from schools

this return all columns(name, age etc... ) combined in to one column as a record

the result set I want to get is

schoolid | teachers name | teachers age | teachers subject  

Solution

  • Use a lateral join:

    select s.id, g.*
    from schools s cross join lateral
         getallteachers(s.id) g