postgres功能需要帮助

问题描述 投票:0回答:1

我正在尝试编写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,即使它有几个要删除。

sql postgresql
1个回答
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); 
© www.soinside.com 2019 - 2024. All rights reserved.