【问题标题】:"return type mismatch in function" Error when creating a table with a function in PostgreSQL“函数返回类型不匹配”在 PostgreSQL 中使用函数创建表时出错
【发布时间】:2019-09-16 17:00:59
【问题描述】:

我对 SQL 很陌生,我正在尝试创建一个返回表的函数,我的代码:

create or replace function ratios_table (datefrom date)
returns table(
    date_of_day date,
    counter integer
    )
as $$
    select
       "creationDate",
        count("interactionId")
    from "UserInteractions"
    where "creationDate" >= $1
    group by "creationDate"
$$
language SQL;

我得到:

声明返回记录的函数返回类型不匹配

如果我像这样在 plpgsql 中写它(当然是 bu...hit):

create or replace function ratios_table (datefrom date)
returns table(
    date_of_day date,
    counter integer
    )
as $$
    begin
        select
            "creationDate",
            count("interactionId")
        from "UserInteractions"
        where "creationDate" >= $1
        group by "creationDate"
    return end;
 $$
language plpgsql;

我明白了

ERROR:  syntax error at or near "return"
LINE 14:     return end;

非常感谢您对新手的帮助和建议。谢谢!

【问题讨论】:

    标签: sql postgresql


    【解决方案1】:

    counter 值是count() 函数的结果,该函数返回一个bigint

    counter 声明为bigint,它将起作用。

    请注意,完整的错误消息会指出这一点

    错误:声明返回记录的函数中的返回类型不匹配
    详细说明:最终语句在列返回 bigint 而不是整数 2.
    上下文:SQL 函数“ratios_table”

    我对 SQL 很陌生,我正在尝试创建一个返回表的函数,我的代码:

    create or replace function ratios_table (datefrom date)
    returns table(
        date_of_day date,
        counter bigint
        )
    as $$
        select
           "creationDate",
            count("interactionId")
        from "UserInteractions"
        where "creationDate" >= $1
        group by "creationDate"
    $$
    language SQL;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多