由于一批错误数据导致数据混乱。现在要写一个脚本执行更改错误数据,大致描述如下:表名暂定为tableA,字段有T,A,b,c,d,现在要把A+B+C+D的累加赋值给T字段。求一段SQL语句一次性更改所有错误数据。
错误数据如下:
T  A  B  C  D   
5  1  2  3  4   
4  2  3  4  5
6  3  4  5  6     
要求解出正确数据如下:
T   A  B  C  D   
10  1  2  3  4   
14  2  3  4  5
18  3  4  5  6      

解决方案 »

  1.   

    update tableA set T=A+B+C+D
    数据量大吗?
      

  2.   

    那就写一个pl/SQL块,让它没5000提交一次
      

  3.   

    cursor c_t is select T,A,b,c,d from tab_name where ....;
    v_t c_t%rowtype;
    i   number;
    begin
     open cursor c_t ;
     loop
       fetch c_t into v_t ;
       exit when c_t%notfound;
       i:i+1;
       update set T=A+B+C+D;
       if i = 10000 then 
          commit;
       i := 0;
       end if; 
     end loop;
     end;
      

  4.   

    declare
    rown int;
    j int := 0;
    begin
      select count(1) into rown from tableA;
      for i in 1 .. rown loop    
          update tableA set T=A+B+C+D;
          j := j + 1;
          if j=50 then
             j := 0;
             commit;
          end if;   
        end loop;
    end;
    /    
      

  5.   

    楼上update set T=A+B+C+D;的时候,最好nvl一下,update set T=nvl(A,0)+nvl(B,0)+nvl(C,0)+nvl(D,0);要不然,一旦一个为空,计算结果就空了.