我正在尝试编写Postgres函数,需要执行以下操作:
DECLARE ids bigint;
Begin
-- save all john's ids. But that seems to save only one id. It may return several
select id_partner INTO ids from tb_partners WHERE name like 'john%';
-- Do a lot of things
-- only after doing things, and that may include add new johns, I need to delete the ones saved at the start of the function.
DELETE FROM tb_partners WHERE id_partner IN (ids);
问题在于,即使要删除一个ID,也只能删除一个ID。
答案 0 :(得分:1)
ids
。 。 。好吧,其中可能不止一个。使用临时表:
create temporary table temp_johns_ids as
select id_partner
from tb_partners
where name like 'john%';
-- Do a lot of things
-- only after doing things, and that may include add new johns, I need to delete the ones saved at the start of the function.
delete from tb_partners
where id_partner in (select id from temp_johns_ids);