"ERROR: query has no destination for result data"



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

我尝试将返回类型更改为int或文本等。查看代码是否在此之外工作,但事实并非如此。我是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

如果要返回某些东西,则需要定义该功能以返回某物(不是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

最新更新