求大大们帮忙写个ORACLE能用的存储过程,我用LOOP循环逐条查询始终有错,
还望哪位高人不吝赐教。
表一
1      2 
abc   abc
abc   ab
abc  abc
ab     a表二
1      2 
abc   ab查询插入表二输出的结果是
1      2 
abc   abc
abc   ab
ab     a1和2是列名 在线等 本人小白 还请莫怪

解决方案 »

  1.   

    merge into tab2 b 
    using (select * from tab1) a
    on a.col1=b.col1 and a.col2=b.col2
    when not matched then
    insert values(b.col1,b.col2)
      

  2.   


    insert into tb2 select distinct a.* from (select * from tb1 minus select * from tb2) a
      

  3.   

    修改一下上面的:
    merge into tab2 b  
    using (select distinct * from tab1) a
    on (a.col1=b.col1 and a.col2=b.col2)
    when not matched then
    insert values(a.col1,a.col2)
      

  4.   

    要是数据比较多的时候怎么办 大大们能不能给写个LOOP循环的 逐条导入 
      

  5.   

    --10g或以上版本支持merge into
    SQL> ed
    已写入 file afiedt.buf  1  merge into b
      2  using (select distinct col1,col2 from a) aa
      3  on(aa.col1=b.col1 and aa.col2=b.col2)
      4  when not matched then
      5  insert
      6* values (aa.col1,aa.col2)
    SQL> /2 行已合并。SQL> select * from b;COL COL
    --- ---
    abc ab
    abc abc
    ab  a
      

  6.   

    汗 多谢上面各位大大, 这个结果确实可以, 但是我的一个朋友让我写个存储过程, 最好是LOOP循环,逐条导入,做一个判断,比如表一第一条数据插入表二,如果表二有这条数据则不插入,如果表二没有这条数据insert,我很无力,写了N次也未写出来 
      

  7.   


    merge into 就能满足了!
      

  8.   

    确实可以 但是老师考的逻辑能力  只能那么回答 大大们帮忙写个LOOP循环呗!万分感谢!!!
      

  9.   


    begin
      for c1 in (select col1,col2 from tab1) loop
        for c2 in (select * from dual where (c1.col1,c1.col2) not in (select col1,col2 from tab2)) loop
          insert into tab2 values c1;
        end loop;
      end loop;
    end;
    /
      

  10.   

    补充一下:
    SQL code
    begin
      i := 0;
      for c1 in (select col1,col2 from tab1) loop
        for c2 in (select * from dual where (c1.col1,c1.col2) not in (select col1,col2 from tab2)) loop
          i := i+1 ;
          insert into tab2 values c1;
          if(mod(i/10)=0)then
             commit;
          end if ;
        end loop;
          commit;
      end loop;
    end;
    /
      

  11.   

    能用merge语句就不要写loop了.loop效率比merge低