Oracle 过程将一个阶段 table 中的所有记录插入主 table
Oracle Procedure to insert all records from one staging table into main table
我写了 Stored Procedure (SP)
,在 SP 内部,2 个 SP 与 table 分开进行 2 个插入。 table 在每个临时和主要 table 中都包含超过 25 列。下面是查询-
create or replace procedure sp_main as
procedure tbl1_ld as
cursor c1 is select * from tmp1;
type t_rec1 is table of c1%rowtype;
v_rec1 t_rec1;
begin
open c1;
loop
fetch c1 bulk collect into v_rec1 limit 1000;
exit when v_rec1.count=0;
insert into tbl1 values v_rec1;
end loop;
end tbl1_ld;
procedure tbl2_ld as
cursor c2 is select * from tmp2;
type t_rec2 is table of c2%rowtype;
v_rec2 t_rec2;
begin
open c2;
loop
fetch c2 bulk collect into v_rec2 limit 1000;
exit when v_rec2.count=0;
insert into tbl2 values v_rec2;
end loop;
end tbl2_ld;
begin
null;
end sp_main;
/
我使用 EXECUTE IMMEDIATE 'insert into tbl1 select * from tmp1';
在两个 SP tbl1_ld & tbl2_ld
中插入而不是使用 cursor
,SP 已编译但没有插入记录。
好吧,您实际上没有 运行 这些程序中的任何一个。你代码的最后几行应该是
<snip>
end tbl2_ld;
begin
tbl1_ld; --> this
tbl2_ld --> this
end sp_main;
/
另一方面,我更喜欢避免 insert into ... select * from
,因为当您修改表的描述并且不修复使用这些表的代码时,它很容易失败。
是的,我知道 - 为所有 25 列命名只是 无聊,但是 - 在我看来 - 这是值得的。所以,我就
begin
insert into tbl1 (id, name, address, phone, ... all 25 columns)
select id, name, address, phone, ... all 25 columns
from tmp1;
insert into tbl2 (id, name, address, phone, ... all 25 columns)
select id, name, address, phone, ... all 25 columns
from tmp2;
end;
换句话说,没有游标、类型、循环……什么都没有。可能是纯 SQL(即没有 PL/SQL)。如果要限制插入的行数,请使用例如... where rownum <= 1000
(如果这就是您使用 limit
子句的原因)。
截至您提到的动态 SQL (execute immediate
):为什么 您会使用它?您编写的代码中没有任何动态内容。
我写了 Stored Procedure (SP)
,在 SP 内部,2 个 SP 与 table 分开进行 2 个插入。 table 在每个临时和主要 table 中都包含超过 25 列。下面是查询-
create or replace procedure sp_main as
procedure tbl1_ld as
cursor c1 is select * from tmp1;
type t_rec1 is table of c1%rowtype;
v_rec1 t_rec1;
begin
open c1;
loop
fetch c1 bulk collect into v_rec1 limit 1000;
exit when v_rec1.count=0;
insert into tbl1 values v_rec1;
end loop;
end tbl1_ld;
procedure tbl2_ld as
cursor c2 is select * from tmp2;
type t_rec2 is table of c2%rowtype;
v_rec2 t_rec2;
begin
open c2;
loop
fetch c2 bulk collect into v_rec2 limit 1000;
exit when v_rec2.count=0;
insert into tbl2 values v_rec2;
end loop;
end tbl2_ld;
begin
null;
end sp_main;
/
我使用 EXECUTE IMMEDIATE 'insert into tbl1 select * from tmp1';
在两个 SP tbl1_ld & tbl2_ld
中插入而不是使用 cursor
,SP 已编译但没有插入记录。
好吧,您实际上没有 运行 这些程序中的任何一个。你代码的最后几行应该是
<snip>
end tbl2_ld;
begin
tbl1_ld; --> this
tbl2_ld --> this
end sp_main;
/
另一方面,我更喜欢避免 insert into ... select * from
,因为当您修改表的描述并且不修复使用这些表的代码时,它很容易失败。
是的,我知道 - 为所有 25 列命名只是 无聊,但是 - 在我看来 - 这是值得的。所以,我就
begin
insert into tbl1 (id, name, address, phone, ... all 25 columns)
select id, name, address, phone, ... all 25 columns
from tmp1;
insert into tbl2 (id, name, address, phone, ... all 25 columns)
select id, name, address, phone, ... all 25 columns
from tmp2;
end;
换句话说,没有游标、类型、循环……什么都没有。可能是纯 SQL(即没有 PL/SQL)。如果要限制插入的行数,请使用例如... where rownum <= 1000
(如果这就是您使用 limit
子句的原因)。
截至您提到的动态 SQL (execute immediate
):为什么 您会使用它?您编写的代码中没有任何动态内容。