我个人觉得写的很好

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,是不管存不存在,都更新,不存在的,结果会被更新成空,但是那条记录还在

oracle批量update的更多相关文章

  1. oracle批量update 转

    需求: 将t2(t_statbuf)表中id和t1(T_Mt)表相同的记录更新进t1表. 1.错误的写法: update table_name t1 set (a,b,c)=( select a,b, ...

  2. oracle 批量更新之update case when then

      oracle 批量更新之update case when then CreationTime--2018年8月7日15点51分 Author:Marydon 1.情景描述 根据表中同一字段不同情况 ...

  3. MyBatis魔法堂:各数据库的批量Update操作

    一.前言   MyBatis的update元素的用法与insert元素基本相同,因此本篇不打算重复了.本篇仅记录批量update操作的sql语句,懂得SQL语句,那么MyBatis部分的操作就简单了. ...

  4. Oracle 批量增加 / 批量跟新

    在使用oracl过程中踩到好多坑,在此记录,也分享给大家. 第一:批量插入 代码一(在为明确表和字段的情况下,动态批量增加): @Insert("<script> " ...

  5. oracle批量更新之使用游标进行分批次更新的5种方式及速度比对

      1.情景展示 一共有22w条数据, 需要将A表的主键更新至B表的指定字段,如何快速完成更新? 2.解决方案 声明: 解决方案不只一种,该文章只介绍快速游标法及代码实现: 两张表的ID和ID_CAR ...

  6. oracle 批量更新表字段

      (一) 将数字替换成汉字 第一步,去重查询 使用distinct关键字先对该字段值进行去重查询,看共有几种情况 --查询指定区间内表停诊字段的值 SELECT DISTINCT T.CLOSE_T ...

  7. oracle 批量更新之将一个表的数据批量更新至另一个表

      oracle 批量更新之将一个表的数据批量更新至另一个表 CreationTime--2018年7月3日17点38分 Author:Marydon Oracle 将一个表的指定字段的值更新至另一个 ...

  8. oracle批量新增更新数据

    本博客介绍一下Oracle批量新增数据和更新数据的sql写法,业务场景是这样的,往一张关联表里批量新增更新数据,然后,下面介绍一下批量新增和更新的写法: 批量新增数据 对于批量新增数据,介绍两种方法 ...

  9. mysql实战之 批量update

    mysql实战之批量update 现阶段我们的业务量很小,要对admin_user表中的relationship字段进行更新,指定id是409.已知409是公司内的一服务中心,需要把该服务中心放到区代 ...

随机推荐

  1. Chisel Tutorial(六)——port

    下面内容根据2015-7-10版的Chisel 2.2 Tutorial整理 port就是硬件单元对外的接口,须要指明方向(输入还是输出).一个port声明的样例例如以下: class Decoupl ...

  2. phpexcel对于中文路径和中文名称的问题(有疑问)

    phpexcel对于中文的文件名无法读取(我本地环境都是utf-8的编码) 是不是win系统识别都是gbk ?(需要把utf-8的字符串改为gbk) $file = "C:\\Users\\ ...

  3. boost thread

    #include <cassert> #include <iostream> #include <boost/ref.hpp> #include <boost ...

  4. Codeforces Round #297 (Div. 2) 525D Arthur and Walls(dfs)

    D. Arthur and Walls time limit per test 2 seconds memory limit per test 512 megabytes input standard ...

  5. CvSplit

    /* possible split in the tree */ typedef struct CvSplit { CvTreeCascadeNode* parent; CvTreeCascadeNo ...

  6. c# 多线程里面创建byte数组发生内存溢出异常求解

    在多线程里面读取一个400多M的Xml文件,首先将其读入FileStream里面,然后,在执行 byte [] bts = new byte[fs.Length]; 这句代码时,出现内存溢出的异常,求 ...

  7. WebRTC编译具体介绍

    WebRTC编译具体介绍--记录+转载 原文地址:http://blog.csdn.net/temotemo/article/details/7056581 WebRTC编译 本人环境: 操作系统:X ...

  8. jqgrid删除多行数据,删不全的解决方案

    功能实现: 删除选中的多条数据 bug: 总是删不干净,比如选中5条执行删除操作,后台全删掉了,可是前台仍然有剩余的几条,再次刷新会不见 错误代码: var ids = $("#grid-t ...

  9. Ubuntu SVN服务器的搭建与配置(转)

    Ubuntu SVN服务器的搭建与配置 一.         安装 sudo apt-get install subversion sudo apt-get install libapache2-sv ...

  10. poj3422 拆点法x->x'建立两条边+最小费用最大流

    /** 题目:poj3422 拆点法+最小费用最大流 链接:http://poj.org/problem?id=3422 题意:给定n*n的矩阵,含有元素值,初始sum=0.每次从最左上角开始出发,每 ...