函数将多个列作为单个列而不是多个列返回

ric*_*chh 17 postgresql

我目前正在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
Run Code Online (Sandbox Code Playgroud)

当前输出如下:

"(6,"test project","inbound","inbound test","processing")"
Run Code Online (Sandbox Code Playgroud)

我该如何才能将结果连接在一起并单独返回每个列项?

先感谢您.

xzi*_*lla 29

你需要调用这样的函数:

select * from foo(6);
Run Code Online (Sandbox Code Playgroud)

这会返回这样的东西:

project_id | project_name | project_type | project_description | project_status
-----------|--------------|--------------|---------------------|----------------
         6 | test project |      inbound |        inbound test |     processing
Run Code Online (Sandbox Code Playgroud)

这是postgres的一个怪癖,它可以被称为双向并给你一个结果.您可能想要检查有关设置返回函数的文档,还有其他方法可以执行此操作.哦,有一个wiki页面,为plpgsql编写,但大多数也适用于sql函数:http://wiki.postgresql.org/wiki/Return_more_than_one_row_of_data_from_PL/pgSQL_functions