【问题标题】:Postgres function, getting "query has no destination for result data", don't want to use table result typePostgres 函数,获取“查询没有结果数据的目的地”,不想使用表结果类型
【发布时间】:2015-12-01 09:40:21
【问题描述】:

我正在编写一个简单的 postgres 函数。这个函数的结果应该是“事件”表的一行。我有以下内容:

create or replace function featured_event() returns setof events as
$$
begin
select events.* from events where featured is true;
end
$$ LANGUAGE plpgsql; 

我不想对列进行硬编码,而是使用现有表中的结构作为返回类型。

不是Function with SQL query has no destination for result data 重复,因为我不想使用table 结果类型。

【问题讨论】:

    标签: postgresql plpgsql


    【解决方案1】:

    使用SQL函数:

    create or replace function featured_event() returns setof events as
    $$
        select events.* from events where featured is true;
    $$ LANGUAGE sql; 
    

    在 plpgsql 中你应该使用return query:

    create or replace function featured_event_plpgsql() returns setof events as
    $$
    begin
        return query select events.* from events where featured is true;
    end;
    $$ LANGUAGE plpgsql; 
    

    【讨论】:

    • return query 是我所需要的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    相关资源
    最近更新 更多