PostgreSQL:调用一个psql函数,返回Select作为参数

时间:2017-10-26 14:30:48

标签: postgresql plpgsql

我用create function xxx(id uuid) returns void as $$创建了一个函数。 现在,我想用select xxx(select id from mytable where ...);来调用它,但这不起作用。我该怎么办?

2 个答案:

答案 0 :(得分:4)

只需处理ID并将其源代码保留在函数调用之外:

select o.name, 
       (select name from geo where id = min(og.geo_id)) as geo_country, 
       (select name from geo where id = max(og.geo_id)) as geo_city
from objects o
inner join objectsgeo og
on og.object_id = o.id
inner join geo g
on g.id = og.geo_id
group by og.object_id;

downvoter的更新:请参阅行动here

答案 1 :(得分:1)

您不能将子查询作为参数传递。一种方法是使用SELECT xxx(s.id) FROM table t1 LEFT JOIN LATERAL (select id from mytable where ... order by ... limit 1) s ON true;

SELECT xxx(s.id)
FROM (subquery) s;

如果您需要在主表和子查询之间进行一些关联,这很有用。

如果只有一个值:

if(pause == "true") {
   response.setContentType("text/plain;charset=UTF-8");
   PrintWriter out = response.getWriter();
try{
    out.println(out.println("Hello World"));
}
catch(Exception ex){
//handel exception here
}
}

或在下面回答。