Postgres-将行ID返回到存储过程变量中会导致错误

问题描述

我有一个存储过程,我试图用它从ID的数组中删除基于ID数组的表的几行,我想返回这些ID并将它们存储在变量中,以便它可以在另一个delete语句中使用。这是我功能的简化部分。

create or replace function "table_a"."deletes"
    (p_ids int[]    
    )
returns int
as $$
declare
    v_directories int[];
begin
    
delete from "table_a"."foo" where "hoo_id" = any(unnest(p_ids)) returning id into v_dirs;
delete from "table_a"."bar" where "foo_id" = any(unnest(v_dirs));

        return 1;
        
        exception
    when others
    then raise exception '% %',sqlstate,sqlerrm;
end;
$$ LANGUAGE plpgsql;
         

这给我一个错误-

“在哪里不允许使用返回设置的功能

我想念什么?

解决方法

改为使用CTE:

with ids as (
      delete from "table_a"."foo"
          where "hoo_id" = any(unnest(p_ids))
          returning id 
     )
delete from "table_a"."bar"
     where "foo_id" in (select id from ids);