因此,在使用触发器时,我通常会获取新插入记录的主键,如下所示。
insert into table1 (pk1, notes) values (null, "Tester") returning pk1 into v_item;
我正在尝试使用相同的概念,但使用 select 语句进行插入。例如:
insert into table1 (pk1, notes) select null, description from table2 where pk2 = 2 returning pk1 into v_item;
注意:
1. table1 上有一个触发器,它会在插入时自动创建 pk1。
2. 由于要插入的表的大小,我需要使用选择插入。
3.插入基本上是记录的副本,因此一次只能插入1条记录。
如果我可以提供更多信息,请告诉我。
最佳答案
我不相信你可以直接通过插入/选择来做到这一点。但是,您可以使用 PL/SQL 和 FORALL 来完成此操作。考虑到表大小的限制,您必须使用 l_limit
平衡内存使用和性能。这是一个例子...
假设此表有 100 行:
create table t (
c number generated by default as identity,
c2 number
);
insert into t (c2)
select rownum
from dual
connect by rownum <= 100;
你可以这样做:
declare
cursor t_cur
is
select c2
from t;
type t_ntt is table of number;
l_c2_vals_in t_ntt;
l_c_vals_out t_ntt;
l_limit number := 10;
begin
open t_cur;
loop
fetch t_cur bulk collect into l_c2_vals_in limit l_limit;
forall i in indices of l_c2_vals_in
insert into t (c2) values (l_c2_vals_in(i))
returning c bulk collect into l_c_vals_out;
-- You have access to the new ids here
dbms_output.put_line(l_c_vals_out.count);
exit when l_c2_vals_in.count < l_limit;
end loop;
close t_cur;
end;
关于oracle - 是否可以在 Insert as select 语句上返回主键 - Oracle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60380206/