gpt4 book ai didi

oracle - 从多个表中删除同一组值

转载 作者:行者123 更新时间:2023-12-04 20:02:08 27 4
gpt4 key购买 nike

我希望不必对所有表一遍又一遍地重复相同的子查询。

例子:

begin
-- where subquery is quite complex and returns thousands of records of a single ID column
delete from t1 where exists ( select 1 from subquery where t1.ID = subquery.ID );
delete from t2 where exists ( select 1 from subquery where t2.ID = subquery.ID );
delete from t3 where exists ( select 1 from subquery where t3.ID = subquery.ID );
end;
/

我发现的另一种选择是:
declare
type id_table_type is table of table.column%type index by PLS_INTEGER
ids id_table_type;
begin
select ID
bulk collect into ids
from subquery;

forall indx in 1 .. ids.COUNT
delete from t1 where ID = ids(indx);

forall indx in 1 .. ids.COUNT
delete from t2 where ID = ids(indx);

forall indx in 1 .. ids.COUNT
delete from t3 where ID = ids(indx);
end;
/

您对这种替代方案有何看法?有没有更有效的方法来做到这一点?

最佳答案

一次创建一个临时表来保存子查询的结果。

对于每次运行,将子查询的结果插入到临时表中。子查询只运行一次,每次删除都很简单:delete from mytable t where t.id in (select id from tmptable); .

完成后截断表格。

关于oracle - 从多个表中删除同一组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34326619/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com