postgresql 查询没有结果数据的目标-类似问题未回答

lb3vh1jj  于 2023-04-29  发布在  PostgreSQL
关注(0)|答案(1)|浏览(104)

我有一个函数,调用3个辅助函数。其中一个帮助函数返回一个整数,其他帮助函数返回void。当我自己调用helper函数时,它会执行并返回一个值。当我试图在 Package 器函数中调用它时,它会抛出错误。
助手功能:

create or replace function create_battery_asset()
returns int
as $$
    declare 
        battery_type_id integer := (select id from asset_type_tb where "type" ilike 'battery');
    
        bat_cap_type integer := (select id from process_type_tb where subtype = 'battery' and subtype2 = 'capacitance');
        bat_res_type integer := (select id from process_type_tb where subtype = 'battery' and subtype2 = 'internal resistance');                                                           -- 
        
        bat_cap_ps integer := (select randchoice(array(select id from process_tb where type_id = bat_cap_type and id > 17)));
        bat_res_ps integer := (select randchoice(array(select id from process_tb where type_id = bat_res_type and id > 17)));
        
    
    begin
        insert into asset_tb("owner", "type_id", "process_id", "serial_number", "common_name", "eol", "units")
            values (current_user, battery_type_id, array[bat_cap_ps, bat_res_ps], (select upper(substr(md5(random()::text), 0, 7))), 'tarot battery', 7000, 'amp-hours');
        
    return (select id from asset_tb where "type_id" = (select id from asset_type_tb where "type" ilike 'battery') order by id desc limit 1)::integer;
        
end;
$$
language plpgsql;

Package 函数

create or replace function replace_battery(uav_id int)
returns void
as $$
    declare 
        -- different ways I have tried this
        -- batt_id int := create_battery_asset()
        -- batt_id int := (select * from create_battery_asset());
        -- batt_id int: (select create_battery_asset());
        batt_id int;
    begin
        -- different ways I have tried this
        -- execute 'select create_battery_asset();' into batt_id;
        -- select into batt_id create_battery_asset();
        -- select create_battery_asset() into batt_id;
        select into batt_id * from create_battery_asset();
        select create_battery_component(batt_id);
        select replace_uav_battery(uav_id, batt_id);
end
$$
language plpgsql;

所有尝试都导致相同的错误消息。提前感谢您的帮助!

kq4fsx7k

kq4fsx7k1#

问题根本不是返回值帮助函数。我需要改变

select create_battery_component(batt_id);
        select replace_uav_battery(uav_id, batt_id);

execute create_battery_component(batt_id);
        execute replace_uav_battery(uav_id, batt_id);

相关问题