将数据从某些行中的列复制到oracle中其他行中的另一列

时间:2012-01-30 14:19:55

标签: oracle

与此问题完全相同:Copy data from one existing row to another existing row in SQL?

但在Oracle中,不支持update ... fromupdate t1, t2

我会用自己的话再说一遍; 我有一张桌子T,看起来像这样:

table

并且如箭头所示,我想复制从r到c = 1到e,其中c = 2,t匹配。

我有select语句来获取我要复制的内容:

select 
  told.t, 
  told.r
from 
  T told 
  inner join 
  T tnew
on 
  told.t= tnew.t
where 
  told.c = 1
  and 
  tnew.c = 2

我只是不知道如何将它们放在一起进行更新。特别是Oracle更新。

2 个答案:

答案 0 :(得分:4)

试试这个:

update T tnew
set tnew.e = (select told.r from T told where told.c = 2 and told.t = tnew.t)
where tnew.c = 1

答案 1 :(得分:0)

听起来像批量收集的时间!不像AB Cade的解决方案那么漂亮,但效率更高。

declare

  c_data is
   select t1.rowid as rid, t2.r
     from my_table t1
     join my_table t2
       on t1.t = t2.t
    where t1.c = 2
      and t2.c = 1
          ;

   type t__data is table of c_data index by binary_integer;
   t_data t__data;

begin

   open c_data;
   loop

      fetch c_data bulk collect into t_data limit 25000;

      exit when t_data.count = 0;

      forall i in t_data.first .. t_data.last loop
         update my_table
            set e = t_data(i).r
          where rowid = t_data(i).rid
                ;

      commit;

   end loop;
   close c_data;

end;
/