oracle数据库中大数据表进行备份

直接上代码–第一版
declare
type cur_sor is ref cursor;
v_cursor cur_sor;
v_rowid SYS.ODCIVARCHAR2LIST;

begin
open v_cursor for select rowid from emp;
loop
fetch v_cursor bulk collect into v_rowid limit 20000;
exit when v_rowid.count = 0;
insert into t_emp_back
select a.* from emp a where exists (select 1 from table(v_rowid) b where a.rowid = b.column_value);
commit;
end loop;
end;

说明部分:
1. SYS.ODCIVARCHAR2LIST 列表变量(类似一个集合)
2. bulk collect ==批量执行操作,只能出现fetch 语句中
3. limit ==表示一次批量插入多少条数据;

第二版:改进过后的执行顺序

declare
type cur_sor is ref cursor;
v_cursor cur_sor; —–申明游标变量
v_rowid sys.odcivarchar2list; —申明类似列表变量
v_sql varchar2(200);
v_insert_sql varchar2(200);
begin
v_sql := ‘select rowid from t_emp_back’;
v_insert_sql := ‘insert /+ append/ into t_emp_back1 select t1.* from t_emp_back t1 where exists (select null from table(:1) t2 where t1.rowid = t2.column_value)’;
open v_cursor for v_sql;
loop
fetch v_cursor bulk collect
into v_rowid limit 30000;
exit when v_rowid.count = 0;
execute immediate v_insert_sql
using v_rowid;
commit;
end loop;
end;

猜你喜欢

转载自blog.csdn.net/qq_39354340/article/details/81387226
今日推荐