现有两张表a,b表a 结构:
co11    col2    col3
A       1 
A       2   
A       3
B       1 
B       2表b结构:
col1    col2
A       'AA'
B       'BB'
现希望将表a更新为:
co11    col2    col3
A       1 
A       2   
A       3       'AA'
B       1 
B       2       'BB'
请问语句该如何写?
感激不禁!!

解决方案 »

  1.   

    declare
      cursor v_rc is
        select t2.col1 as a, t2.col2 as b, t1.m as c
          from t2, (select col1 c, max(col2) as m from t group by col1) t1
         where t1.c = t2.col1;
      i v_rc%rowtype;
    begin
      open v_rc;  loop
        fetch v_rc
          into i;
        exit when v_rc%notfound;
        update t
           set col3 = i.b
         where t.col1 = i.a
           and t.col2 = i.c;
      end loop;
      commit;
    Exception
      when others then
        rollback;
        Dbms_Output.put_line('Error' || sqlcode);
    end;
      

  2.   

    表T是你的Table1——a,表T2是你的Table2——b
      

  3.   


      1  update a set col3=(select b.col2 from b where a.col1=b.col1 and a.col2=
      2  (select col2 from
      3  (select row_number()over(partition by col1 order by col2 desc) rn ,
      4  col2,col1 from a ) c
      5* where rn=1 and a.col1=c.col1))
    SQL> /已更新5行。SQL> select * from a;C       COL2 COL3
    - ---------- ----------
    a          1
    a          2
    a          3 aaa
    b          1
    b          2 bbb