zoukankan      html  css  js  c++  java
  • oracle批量update 转

    我个人觉得写的很好

    http://blog.csdn.net/wanglilin/article/details/7200201

    需求:

    将t2(t_statbuf)表中id和t1(T_Mt)表相同的记录更新进t1表。

    1.错误的写法:

     1 update table_name t1 set (a,b,c)=( select a,b,c from table_name_2 t2 where t1.a=t2.a);  

    这种写法,会更新t1表中的所有行:如果t1.a=t2.a的,就更新t2中查出的记录进t1;如果t1.a<>t2.a的,t1中的记录会被更新成空(null)。

    正确的写法:

    1 update table_name t1 set (a,b,c)=( select a,b,c from table_name_2 t2 where t1.a=t2.a)
    2 where exists(select 1 from table_name_2 t2 where t1.a=t2.a);  

    解析:

    正确的写法,就是在后面加了一句 where exists(select 1 from table_name_2 t2 where t1.a=t2.a);

    这句话的意思是:如果存在t1.a=t2.a,就更新,否则,不更新,所以不会导致t1表中所有的记录都被更新。

    例:

    update table_name_1 set (a,b) = (select 1,2 from dual where 1=2);

    这个结果会把table_name_1中的记录全部更新成空(null),因为后面1=2不成立。

    总结:

    update时,要弄清限定条件,要测试!

    我的测试语句:

    1 update my_time_test1 t1 set (MDATE,DISCRIPT) =(select MDATE,DISCRIPT from   
    2 my_time_test t2 where t1.DISCRIPT=t2.DISCRIPT) where exists (select 1 from   
    3 my_time_test t2 where t1.DISCRIPT=t2.DISCRIPT);  

    我的业务语句:

    1 update T_Mt t1 set (Stat,OStat,RptTime) =(  
    2 select Stat,Stat,RptTime from t_statbuf t2 where t1.MsgId=t2.MsgId) where exists(  
    3 select 1 from t_statbuf t2 where t1.MsgId=t2.MsgId);  --如果存在t1和t2相等的,就更新。不加where exists,是不管存不存在,都更新,不存在的,结果会被更新成空,但是那条记录还在  
  • 相关阅读:
    59
    58
    57
    56
    55
    54
    53
    转 Using $.ajaxPrefilter() To Configure AJAX Requests In jQuery 1.5
    jquery用正则表达式验证密码强度
    什么是高内聚、低耦合?(转载)
  • 原文地址:https://www.cnblogs.com/wolfplan/p/4004612.html
Copyright © 2011-2022 走看看