我的思路是这样的:
  先在数据库中按照表a的结构建张表b,然后插入数据。然后对表b中的数据做游标的循环,一行行的插入。插入前取表a的序号的最大值,如果表b中的学号不在表a中,就进行插入。同时,序列号增加1。一次循环结束后,再进行下一次的循环。
   我这样的缺点是,效率会很低的,因为是一行行循环的,满足不了你的第四个要求。

解决方案 »

  1.   

    我建议如下方法:
    1、 在数据库中建张表b;
    2、删除表b中序号与表a序号相同的数据:delete from 表b where 序号=(select 序号 from 表a)
    3、查询表a序号最大值:select max(序号) from 表a
    4、修改表b序号字段为表a的 max序号+1开始的递增字段:update 表b set 序号=rownum+max+1
       注:max为表a序号最大值
    5、将表b整体插入表a:insert into 表a select * from 表b 
    分步骤简单、方便,而且这些操作分步执行,不需要循环,效率方面也没啥问题。
    如果你担心数据量大,可以分批。如有20万条,你可以分多次插入。如:10万一次。
       insert into 表a select * from 表b where 表b.序号<=max+100001;
       insert into 表a select * from 表b where 表b.序号>max+100001;
    这样就把20万条数据分两次插入。
      

  2.   

    同意 楼上 zhaoyuup 的,不过2有点小问题2、删除表b中序号与表a序号相同的数据:delete from 表b where 序号 in(select 序号 from 表a)
      

  3.   


    -- Create sequence 
    create sequence SEQ_TABLEA
    minvalue 1
    maxvalue 999999999999999
    start with 101
    increment by 1
    cache 20;
    --插入数据
    insert into tablea(序号,学号,考勤)
    select seq_tablea.nextval,学号,考勤 from tableb b
    where not exists (select 1 from tablea a where a.学号=b.学号)
      

  4.   

    http://blog.csdn.net/yole_grise/article/details/15337973用Merge into,例子是sql server2008的,oracle直接用,差不多少! 
      

  5.   

    Merge +自治事务是否可以满足?