【问题标题】:Copy data from a column in some rows to another column in other rows in oracle在oracle中将数据从某些行中的列复制到其他行中的另一列
【发布时间】:2012-01-30 22:56:17
【问题描述】:

和这里的问题完全一样:Copy data from one existing row to another existing row in SQL?

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

我会用我自己的话在这里重复一遍; 我有一个表 T,它看起来像这样:

如箭头所示,我想将所有内容从 c = 1 的 r 复制到 c = 2 的 e,并与 t 匹配。

我有选择语句来获取我想要复制的内容:

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 更新。

【问题讨论】:

    标签: oracle


    【解决方案1】:

    试试这个:

    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
    

    【讨论】:

    • 嗯...谢谢。现在我知道答案似乎容易多了!
    【解决方案2】:

    听起来是时候进行大量收集了!不如 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;
    /
    

    【讨论】:

      猜你喜欢
      • 2018-02-27
      • 1970-01-01
      • 1970-01-01
      • 2020-05-17
      • 2022-01-01
      • 1970-01-01
      • 2021-06-13
      • 2019-01-10
      • 2023-03-16
      相关资源
      最近更新 更多