sql - “错误:查询没有结果数据的目的地”
问题描述
我正在尝试创建这样的函数:
我尝试将返回类型更改为 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
解决方案
如果你想返回一些东西,你需要定义函数来返回一些东西(不是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);
推荐阅读
- oracle - PL/SQL 函数返回最小数
- android - 如何使用 android 在 iroha 上授予权限?
- ksh - 删除早于当天开始的文件
- sql-server - 从 DB2 中的表更新 DB1 的表时,找不到多部分标识符错误
- r - 在条形图中使用离散颜色映射,并在 R 中的箱形图上使用抖动
- javascript - 如何在 Typescript 中推送数组中的元素
- ssl - 尝试使用 2 路 SSL 建立 Kafka 但出现错误
- android - 自定义通知 RemoteView 在向左或向右滑动时关闭
- java - JAVA:当日志文件要翻转时在应用程序中暂停
- amazon-web-services - Redshift 工作负载管理规则含义