我目前正在postgresql 9.04中编写一个函数,试图使用一个将在select语句中使用的变量,然后返回结果。
我提出的语句很简单,而且有效;但是,所有列都输出到一个列,而不是多个列。
以下是我的功能:

create or replace function foo(IN pID integer, OUT project_id integer, OUT project_name    text, OUT project_type text, OUT project_description text, OUT project_status text)
returns setof record as

$$
select project_id, project_name, project_type, project_description, project_status from     t_projects
where project_id = $1;
$$

LANGUAGE SQL;


select foo(6) -- runs function

当前输出如下:
"(6,"test project","inbound","inbound test","processing")"

如何才能使结果不连接在一起并分别返回每个列项?
提前谢谢你。

最佳答案

您需要这样调用函数:

select * from foo(6);

它将返回如下内容:
project_id | project_name | project_type | project_description | project_status
-----------|--------------|--------------|---------------------|----------------
         6 | test project |      inbound |        inbound test |     processing

博士后的一个怪癖是,它可以被称为两种方式,并给你一个结果。您可能还想进一步检查集合返回函数的文档,还有其他方法可以做到这一点。哦,上面有一个wiki页面,是为plpgsql编写的,但大多数也适用于sql函数:http://wiki.postgresql.org/wiki/Return_more_than_one_row_of_data_from_PL/pgSQL_functions

关于postgresql - 函数将多列作为单列而不是多列返回,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7072021/

10-11 17:37
查看更多