【问题标题】:postgresql function returns multiple columnspostgresql 函数返回多列
【发布时间】:2017-05-02 20:09:37
【问题描述】:

我对 Postgres 中创建的函数的参数传递过程感到困惑:

create type IncorrectRecord as (pattern_number integer, uoc_number integer);

create or replace function text1(pattern text, uoc_threshold integer) 
    returns  setof IncorrectRecord
as $$
begin
return next count(v1.code) as pattern_number, count(v2.code) as uoc_number
from (select * from q1_1 where code like pattern) as v1, (select
 * from q1_1 where code like pattern and uoc > uoc_threshold) as v2;
return;
end
$$ language plpgsql;

我修改了一些,没有参数错误但还是不行。 当我用

测试它时
select * 
from test1('ECO%', 8)

错误:函数返回两列。

类型有什么问题吗?我该如何解决?

【问题讨论】:

  • returns IncorrectRecord - 什么是IncorrectRecord
  • 如果你想检索这对值,其中第一个是code like pattern 的计数,第二个是code like pattern and uoc > uoc_threshold 的计数 - 你使用了错误的方法。回答你的问题,简而言之,在DDL中使用参数是不可能的。
  • 这是作业吗?看起来非常类似于:stackoverflow.com/questions/43735008/postgresql-functions

标签: postgresql plpgsql


【解决方案1】:

正如消息所说,您要返回两列。改为返回复合类型:

return next (
    count(v1.code) as pattern_number, count(v2.code) as uoc_number
)::IncorrectRecord

【讨论】:

    猜你喜欢
    • 2014-12-15
    • 2020-08-03
    • 2010-10-19
    • 2012-01-09
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    相关资源
    最近更新 更多