Oracle数据迁移-系统数据合并笔记

时间:2023-12-09 16:18:07

创建临时表:execute immediate 'sql';

通过临时表和关联查询解决循环处理效率低下,大数据操作移植时时间太长的问题。

结构相同的系统数据库表移植,案例如下:

create or replace procedure jk_trade_transfer authid current_user IS

create_sql     );
update_sql     );
insert_sql     );
V_info_id  NUMBER;
info_count NUMBER;
begin
  --一、迁移trade表
  --创建截止迁移之时起的交款交易的备份表
  create_sql:''';
  execute immediate create_sql ;

  --2、创建获取交款申请编号重复的交款交易数据表
  create_sql:''';
  execute immediate create_sql ;

  --3、生成新的申请编号并创建映射原申请编号的对应关系表
  --获取迁移数据的需要重新生成申请编号的数量
  ) ');
  --获取序列最大值
  ), ), ')INTO V_info_id from keytable where key_name = 'batchno';
  --创建主键映射表
  create_sql:='create table jk_trade_map as select batch_no ,ROWNUM +' ||V_info_id ||' as batch_no_new from jk_trade_1 order by batch_no';
  execute immediate create_sql ;
  --更新序列值
  update_sql:='update keytable set value = value +'|| info_count ||'where key_name='||'''batchno''';
  execute immediate update_sql ;
  --将不需要重新生成申请编号的数据插入映射表
  insert_sql:='insert into jk_trade_map select batch_no,batch_no batch_no_new from jk_trade jt where not exists(select jk.batch_no from jk_trade_1 jk where jk.batch_no=jt.batch_no)';
  execute immediate insert_sql ;

  --4、创建导入数据表
  create_sql:='create table jk_trade_copy as select itm.batch_no_new as batch_no, it.tran_type,it.tran_status, it.reg_operid, it.reg_opername,'||
   ' it.reg_date, it.reg_time, it.finish_date, it.finish_time, it.tran_amt,it.tran_area, it.tran_count, bi.info_id_new sect_id, it.crt_org_id, it.dist_id,'||
   ' it.req_notify_no, it.res_notify_no, it.tran_group,it.tran_mode,'||'''数据迁移'''||' otr_remark,it.id,itm.batch_no orgin_batch_no from info_trade it '||
   ' join jk_trade_map itm on it.batch_no=itm.batch_no left join base_info_map bi on bi.info_id=it.sect_id  order by it.batch_no';
  execute immediate create_sql ;

  --5、导入交款交易数据
  insert_sql:='insert into trade  select * from jk_trade_copy';
  execute immediate insert_sql ;

  COMMIT;
  ---迁移trade表 end

  --二、迁移pay_detail表
  --1、创建交款清册导入表
  create_sql:='create table pay_detail_copy as select t.batch_no_new batch_no,t.fund_new fund,bim.info_id_new info_id,p.info_area,p.dev_should,p.own_should,p.dev_amt,p.own_amt,p.tran_amt,'||'''数据迁移'''||' p.dec_remark from pay_detail p'||
              ' join jk_trade_map t on p.batch_no=t.batch_no'||
              ' left join base_info_map bim on bim.info_id=p.info_id'||
              ' left join acct t on t.fund=p.fund';
  execute immediate create_sql ;

  --2、导入交款清册数据
  insert_sql:='insert into pay_detail  select * from pay_detail_copy';
  execute immediate insert_sql ;   

  COMMIT;
  --三、迁移pay_sum表
  --1、创建获取交款组编号重复的交款交易数据表
  create_sql:='create table jk_trade_2 as select distinct t.batch_no,t.tran_mode from trade@jn t where exists (select td.batch_no from pay_sum td where td.batch_no=t.tran_group)';
  execute immediate create_sql ;

  --2、生成新的申请编号并创建映射原申请编号的对应关系表
  --获取迁移数据的需要重新生成申请编号的数量
  ) INTO info_count from (select distinct t.batch_no,t.tran_mode from trade@jn t where exists (select td.batch_no from pay_sum td where td.batch_no=t.tran_group));
  --获取序列最大值
  )), ), ')INTO V_info_id from keytable where key_name = 'batchno';
  --创建主键映射表
  create_sql:='create table pay_sum_map as select batch_no ,ROWNUM +' ||V_info_id ||' as batch_no_new from jk_trade_2 order by batch_no';
  execute immediate create_sql ;

  --将交易表中涉及tran_group重新生成的更新
  update_sql:''';
  execute immediate update_sql ;

  --更新序列值
  update_sql:='update keytable set value = value +'|| info_count ||'where key_name='||'''batchno''';
  execute immediate update_sql ;
  --将不需要重新生成申请编号的数据插入映射表
  insert_sql:='insert into pay_sum_map select batch_no,batch_no batch_no_new from pay_sum jt where not exists(select jk.batch_no from jk_trade_2 jk where jk.batch_no=jt.batch_no)';
  execute immediate insert_sql ;     

  --3、创建交款清册导入表
  create_sql:='create table pay_sum_copy as select t.batch_no_new batch_no,p.pay_type,'||'''数据迁移'''||' sum_remark from pay_sum p'||
              ' join pay_sum_map t on p.batch_no=t.batch_no';
  execute immediate create_sql ;

  --4、导入交款清册数据
  insert_sql:='insert into pay_sum  select * from pay_sum_copy';
  execute immediate insert_sql ;   

end jk_trade_transfer;