目前需求是根据源表数据如salary>10000的数据插入目标表中,目标表根据需要指定,且插入指定N条数据后提交。
对于此类问题可以用insert into select方法但需要按指定N条数据提交,说明表中数据量很大,插入时应批量提取,再按
指定条数插入新表,且批量提取数据时也应根据数据大小分批提取,于是考虑在动态sql中使用bulk collect into + limit的方法
具体实例如下
drop table emp_bak;
create table emp_bak
as select first_name
,salary
,rownum rnid
from employees
where 1 != 1;
declare
SOURCE_TABLE varchar(100);
TAG_A VARCHAR(100);
C_COUNT VARCHAR(100);
v_query_sql varchar2(500);
v_query_rn varchar2(100);
type delArray1 is table of hr.employees.first_name%type index by binary_integer;
type delArray2 is table of hr.employees.salary%type index by binary_integer;
type delArray3 is table of hr.employees.employee_id%type index by binary_integer;
first_name delArray1;
salary delArray2;
rnid delArray3;
rnd number;
TYPE i_cursor_type IS REF CURSOR;
my_cursor i_cursor_type;
begin
SOURCE_TABLE := 'employees';
TAG_A := 'emp_bak';
C_COUNT := 10;
v_query_rn :=
'select count(*)
from '||SOURCE_TABLE||'
where salary > 10000';
v_query_sql :=
'select first_name
,salary
,rownum
from '||SOURCE_TABLE||'
where salary > 10000';
open my_cursor for v_query_sql;
execute immediate v_query_rn into rnd;
for i in 1..rnd loop
fetch my_cursor bulk collect into first_name,salary,rnid limit 10;
for i in 1..first_name.count
loop
execute immediate 'insert into hr.'|| TAG_A||' values (:1, :2, :3)'
using first_name(i),salary(i),rnid(i);
end loop;
end loop;
close my_cursor;
end;
select count(*) from emp_bak;