需要的postgres函数帮助

时间:2019-03-18 20:11:27

标签: sql postgresql

我正在尝试编写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。

1 个答案:

答案 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);