You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PostgreSQL PL/pgSQL函数调用报错:无结果目标的排查求助

解决PostgreSQL中"query has no destination for result data"错误

错误原因

你遇到的错误根源有两点:

  1. 调用返回void的函数时使用了select关键字,PostgreSQL会尝试将select的结果存入变量,但void类型无返回值,导致报错。
  2. 获取create_battery_asset()返回值的写法冗余,没必要用select * from的形式。

修正方案

  1. 调用返回void的create_battery_component()和replace_uav_battery()时,直接写函数调用语句(或用perform关键字),不要加select前缀。
  2. 获取create_battery_asset()的返回值,用简单的赋值或select ... into语法即可。

修正后的包装函数

create or replace function replace_battery(uav_id int)
returns void
as $$
    declare 
        batt_id int;
    begin
        -- 直接调用函数并赋值
        batt_id := create_battery_asset();
        
        -- 调用返回void的函数,用perform忽略返回结果
        perform create_battery_component(batt_id);
        perform replace_uav_battery(uav_id, batt_id);
end;
$$
language plpgsql;

也可以使用select into的写法,效果完全一致:

create or replace function replace_battery(uav_id int)
returns void
as $$
    declare 
        batt_id int;
    begin
        select create_battery_asset() into batt_id;
        
        perform create_battery_component(batt_id);
        perform replace_uav_battery(uav_id, batt_id);
end;
$$
language plpgsql;

补充说明

在PL/pgSQL中,调用无返回值的函数,或调用有返回值但不需要使用结果的函数时,perform是标准用法,它会执行函数并自动忽略返回结果,避免出现"no destination for result data"类错误。

内容的提问来源于stack exchange,提问作者darrahts

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 22:03:14