0

I have a stored procedure which I'm trying to use to delete several rows of a table based on an array of id's, from the rows that are deleted I want to return those id's and store them in a variable so that it can be used in another delete statement. Here's a reduced segment of my function.

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;
         

This gives me an error of -

'set-returning functions are not allowed in WHERE'

What am I missing?

1 Answer 1

1

Use a CTE instead:

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Am I able to use the 'ids' for multiple deletes with this methods? I've edited the original post to demonstrate what I mean with your approach. Thanks.
Nevermind, my formatting was incorrect, got it thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.