微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

oracle 批量插入时,如何去除重复数据

用储存过程批量抽取一个视图的数据,插入到一个新建的表,视图数据有2.4亿,昨天抽取到6千万就卡住了,不知道什么原因,想继续执行这个存储过程,想请问加什么条件来避免插入那些已经插入过的数据

视图上有唯一性字段XH

储存过程如下

create or replace procedure up_table as

type a is table of new_table%rowtype;

in_data a;

i number;

cursor c is select * from fcd_ci_gps@dblink;

begin

open c;

loop

fetch c bulk collect into in_data limit 5000;

forall i in 1..in_data.count

insert into new_table values in_data(i);

commit;

exit when in_data.count=0;

end loop;

close c;
end;






最近刚做了一个你说的类似需求:

我的业务需求是,
从oracle数据库获取数据,然后同步到sqlserver中。

首先是配置两个数据库间的连接设置。
我是sqlserver 连接oracle 配置sqlserver的链路服务器就OK。

下面是存储过程的内容了:

1. 创建临时表。

通过远程连接,insert into 临时表 select 远程表 。
获取数据先到本地,。

然后用 临时表的数据,跟你本地业务表的数据进行对比。
查询不通的数据。

Java代码
  1. --(1)远程读取NC需求计划,分组汇总数据后,插入到临时表#tmp_pl_plan中。
  2. set@InsertStrsql=@InsertStrsql+@tmpStrsql;
  3. print(@InsertStrsql);
  4. exec(
  5. select@tmpCont=count(1)from#tmp_pl_plan;
  6. --state:0新增、1修改2删除
  7. 2)用本地数据与临时表中的数据,进行对比,更新本地表中计划数量与临时表中不相等的记录.
  8. updatetsett.plnum=a.plnum,t.state=1
  9. from#tmp_pl_plana,NC_PL_PLANt
  10. wherea.factorycode=t.factorycodeanda.weldingdate=t.weldingdate
  11. anda.divisions=t.divisionsanda.zzmadeline=t.zzmadeline
  12. anda.zzweldingwayCode=t.zzweldingwayCodeanda.zzmadelinetypeCode=t.zzmadelinetypeCode
  13. anda.convertedcode=t.convertedcodeanda.ncfprocode=t.ncfprocode
  14. andt.plnum!=a.plnum
  15. andt.weldingdate>=@fbegdateandt.weldingdate<=@fenddate
  16. 3)对比数据,查找本地表中存在,但是临时表中不存在的记录,然后修改本地表中的数量=0,state=3表示删除
  17. updatetsett.plnum=2
  18. fromNC_PL_PLANt
  19. wheret.weldingdatebetween@fbegdateand andnotexists(
  20. select1from#tmp_pl_planawherea.factorycode=t.factorycodeanda.weldingdate=t.weldingdate
  21. );
  22. 4)对比数据,新增临时表中不存在于当前表的数据
  23. --deleteNC_PL_PLAN;
  24. insertintoNC_PL_PLAN
  25. select*from#tmp_pl_plant
  26. 1fromNC_PL_PLANawherea.factorycode=t.factorycodeanda.weldingdate=t.weldingdate
  27. anda.weldingdate>=@fbegdateanda.weldingdate<= )
  28. orderbyt.weldingdatedesc;

最近刚做了一个你说的类似需求:

我的业务需求是,
从oracle数据库获取数据,然后同步到sqlserver中。

首先是配置两个数据库间的连接设置。
我是sqlserver 连接oracle 配置sqlserver的链路服务器就OK。

下面是存储过程的内容了:

1. 创建临时表。

通过远程连接,insert into 临时表 select 远程表 。
获取数据先到本地,。

然后用 临时表的数据,跟你本地业务表的数据进行对比。
查询不通的数据。

Java代码
  1. 1)远程读取NC需求计划,分组汇总数据后,插入到临时表#tmp_pl_plan中。
  2. @tmpStrsql;
  3. @InsertStrsql);
  4. 1)from#tmp_pl_plan;
  5. 2删除
  6. 2)用本地数据与临时表中的数据,进行对比,更新本地表中计划数量与临时表中不相等的记录.
  7. 1
  8. wherea.factorycode=t.factorycodeanda.weldingdate=t.weldingdate
  9. anda.divisions=t.divisionsanda.zzmadeline=t.zzmadeline
  10. anda.zzweldingwayCode=t.zzweldingwayCodeanda.zzmadelinetypeCode=t.zzmadelinetypeCode
  11. anda.convertedcode=t.convertedcodeanda.ncfprocode=t.ncfprocode
  12. andt.plnum!=a.plnum
  13. @fenddate
  14. 3表示删除
  15. 2
  16. fromNC_PL_PLANt
  17. andnotexists(
  18. 1from#tmp_pl_planawherea.factorycode=t.factorycodeanda.weldingdate=t.weldingdate
  19. );
  20. 4)对比数据,新增临时表中不存在于当前表的数据
  21. --deleteNC_PL_PLAN;
  22. insertintoNC_PL_PLAN
  23. select*from#tmp_pl_plant
  24. 1fromNC_PL_PLANawherea.factorycode=t.factorycodeanda.weldingdate=t.weldingdate
  25. )
  26. orderbyt.weldingdatedesc;



第一:merge into 就是最快的表更新方案了,merge into 能够去除重复数据,插入新数据,我不知道你为什么不用merge into。

第二: 如果你不信任merge into,那么你可以在被更新的数据表中对唯一标识的列建立索引(index),这样你在直接使用游标将一个表对另外一个表更新的时候会快很多很多。

原文地址:https://www.jb51.cc/oracle/208961.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐