首页 文章

从函数PostgreSQL而不是记录返回多个列和行

提问于
浏览
1

我正在网上阅读关于PostgreSQL上的函数并返回结果在这个链接中:

我写过这个函数:

create or replace function brand_hierarchy(account_value int)
  RETURNS table (topID INTEGER, accountId INTEGER, liveRowCount bigint,archiveRowCount bigint)
  AS
$BODY$
  SELECT * FROM my_client_numbers
where accountId  = coalesce($1,accountId);
$BODY$
LANGUAGE sql;

哪个工作并将结果返回到单列记录类型 . 请注意,可能会返回多行 .

现在响应是:

record
(1172,1172,1011,0)
(1172,1412,10,40)
.....

我希望我的结果不是作为记录而是作为多列

|---------|---------|------------|----------------|
| topID   |accountId|liveRowCount|archiveRowCount |
|---------|---------|------------|----------------|
| 1172    |1172     | 1011       |  0             |
| 1172    |1412     | 10         |  40            |

有没有办法从PostgreSQL函数返回多个列

2 回答

  • 4

    返回表(或setof)的函数应该在FROM子句中使用:

    select * 
    from brand_hierarchy(1234)
    
  • 1

    我能够通过此查询按预期看到它:

    SELECT * FROM brand_hierarchy (id)
    

相关问题