首页 > 解决方案 > “错误:查询没有结果数据的目的地”

问题描述

我正在尝试创建这样的函数:

我尝试将返回类型更改为 int 或 text 等,以查看代码是否在此之外工作,但事实并非如此。我是 PostgreSQL 的初学者,所以如果我遗漏了一些明显的东西,请不要苛刻。

create or replace function date_select(i INT) returns void as
$$ 
    begin
        select * from dwh_stg.stg_dce_gift where gift_id = i;
    end
$$ language plpgsql

select date_select(16940)

SQL 错误 [42601]:

ERROR: query has no destination for result data
Hint: If you want to discard the results of a SELECT, use PERFORM instead.
Where: PL/pgSQL function date_select(integer) line 3 at SQL statement

标签: sqlpostgresqlstored-functionsset-returning-functions

解决方案


如果你想返回一些东西,你需要定义函数来返回一些东西(不是void

显然您想从表中返回多行stg_dec_gift,为此您需要将函数定义为returns setof dwh_stg.stg_dce_gift. 对于一个封装查询的简单函数,不需要使用 PL/pgSQL,一个普通的 SQL 函数就可以了:

create or replace function date_select(i INT) 
  returns setof dwh_stg.stg_dce_gift --<<  here
as
$$ 
  select * 
  from dwh_stg.stg_dce_gift 
  where gift_id = i;
$$ 
stable
language sql;

然后在FROM零件中使用它:

select *
from date_select(16940);

在线示例:https ://rextester.com/WYDCE44062


推荐阅读