oracle数据库:   现在又两张表a和b,现在就是想把a表中的多条记录插入到b表中,然而插入到b表中的记录在a表中要把它删除掉  (也就是a表中的这些多条记录要删除),该怎样写啊,sql或存储过程。谢谢各位帮忙!小妹感激不尽。

解决方案 »

  1.   

    1、插入数据的条件是什么,删除时的条件还是什么啊。
    begin
      insert into 表b as select * from 表a where 条件1;
      delete from 表a where 条件1;
    end;
    2、或者插入数据时把插入的数据的主键标示放到一个临时表或临时变量中,插入成功后按照临时记录删除
    3、或者使用触发器
      

  2.   

    Insert into B(字段) select 字段 from A where...
    --再delete A where...
      

  3.   

    先insert into b 后delete b,两者的where一样就好了
      

  4.   

    把需要插入的数据的rowid拿出来,插入完了之后根据rowid直接删除。
      

  5.   

    4楼已经给出正解,依照做就是了。 最后,记得加上 commit。
      

  6.   

    首先判断 select count(1) into 变量 from a where  create_date>=trunc(add_months(sysdate,-2)) and rownum=1;如果变量 > 0 
    begin
    insert into b
    select 字段 from a where create_date>=trunc(add_months(sysdate,-2));
    delete from a where  create_date>=trunc(add_months(sysdate,-2));
    commit;
    end数据量大,a的create_date有索引吧。
    如果可以以create_date建月分区,两个月的数据直接插入,和用alter 删分区应该会比较快吧。
      

  7.   


    --很简单,用PL/SQL
    BEGIN
        INSERT INTO(col1,col2) A SELECT col1,col2 FROM B WHERE [CONDITIONS];
        DELETE FROM A WHERE [CONDITIONS];
        COMMIT;
    END;
    /