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

  • 本文关键字:返回 函数 单个列 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

当前输出如下所示:

"(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

这是postgres的一个怪癖,它可以以两种方式调用并给您一个结果。您可能需要更多地查看set返回函数的文档,也有其他方法可以做到这一点。哦,有一个关于它的wiki页面,为plpgsql编写,但大多数也适用于sql函数:http://wiki.postgresql.org/wiki/Return_more_than_one_row_of_data_from_PL/pgSQL_functions

最新更新