我目前正在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

博士后的一个怪癖是,它可以双向调用,并给你一个结果.您可能还需要判断集合返回函数上的文档,还有其他方法可以做到这一点.哦,上面有一个为plpgsql编写的wiki页面,但大多数也适用于sql函数:http://wiki.postgresql.org/wiki/Return_more_than_one_row_of_data_from_PL/pgSQL_functions

Postgresql相关问答推荐

将real[]数组作为完整的浮点值从postquist返回

PostgreSQL散列连接与嵌套循环和散列索引

创建发布性能

在PostgreSQL中是否有与SQLite R*Tree类似功能?

Postgres 对 WHERE 子句并行使用两个索引

求和直到达到一个值 postgresql

使用FastAPI和PostgreSQL进行测试

Postgres >= 和 <= 具有特殊字符的行为

是否可以短时间运行 VACUUM FULL 并获得一些好处?

EF Core添加迁移目标postgreeSQL返回无法加载类型'Microsoft.EntityFrameworkCore.Storage.IRelationalValueBufferFactoryFactory

PostgreSQL - 列出模式中的所有唯一约束

PostgreSQL bytea 网络流量双倍预期值

使用 ON CONFLICT 从 INSERT 返回行,无需更新

PostgreSQL CASE 在函数中的使用

没有查询要创建的in ... error

错误:must be owner of language plpgsql

如何在数据库表中查找重复条目?

如何判断 PostgreSQL 事务中的待处理操作

使用枚举与布尔值?

如何在 JPA 中使用 Postgres JSONB 数据类型?