A表customers和B表tmp_cust_city有3个相同字段, customer_id,city_name,customer_type 现要根据b表更新a表

更新一个字段情况:

update customers a

set city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)

where exists (select 1 from tmp_cust_city b where b.customer_id=a.customer_id)

//最后的where exists条件不可省,因为如果省去,就会更新a表中所有记录,但是a表中存在的b表中无法匹配的记录。这些记录的city_name就会被设成null,产生重大数据事故。

除非使用nvl(exp1,exp2)函数:当一个表达式为null时,采用第二个表达式

update customers a

set city_name=nvl((select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),a.city_name) //这样可保持未匹配b表的a中的city_name保持a. city_name原值而不被null掉。

此外还有NVL2函数的格式如下:NVL2(expr1,expr2, expr3)exp1不为null,则采用exp2, null则采用exp3(2016.4.21更正,验证)

 

更新多个字段情况:

update customers a

set (city_name,customer_type)=(select b.city_name,b.customer_type from tmp_cust_city b where b.customer_id=a.customer_id)

where exists (select 1 from tmp_cust_city b where b.customer_id=a.customer_id )

where exists功能与上面单字段相同。

还可以用视图的形式update更新

update (select a.city_name,b.city_name new_name

from customers a,

tmp_cust_city b

where b.customer_id=a.customer_id

)

set city_name=new_name

但前提是 A(customer_id) b(customer_id)必需是unique index或primary key。否则会报错:

ORA-01779, "cannot modify a column which maps to a non key-preserved table" 无法修改与非键值保存表对应的列

以上无论单字段还是多字段更新,都有个前提就是b表中的b.customer_id必须是唯一的,否则出错

例子:

//以下更新两张表的sql语句有内外两层where语句,但作用不一样,内层的where用于限定f2表中哪些(条)记录来更新f1,外层where条件用于限定f1中哪些记录需要更新

string sql = string.Format(@"update {0}.airway_point f1

set (airway_point_type_id,txt_name,code_id,geo_lat,geo_long,val_mag_var,code_fir,code_icaocode,code_fir_eag,code_icaocode_eag,code_icao,val_freq,val_elev,code_channel,code_type_waypoint,name_eng,txt_rmk,val_force_report,isvalid,modify_reason_id,modify_reason_add,uom_freq,code_sx,modified,date_lastmodify,val_current_state)

=

(

select f2.airway_point_type_id,f2.txt_name,f2.code_id,f2.geo_lat,f2.geo_long,f2.val_mag_var,f2.code_fir,f2.code_icaocode,f2.code_fir_eag,f2.code_icaocode_eag,f2.code_icao,f2.val_freq,f2.val_elev,f2.code_channel,f2.code_type_waypoint,f2.name_eng,f2.txt_rmk,f2.val_force_report,f2.isvalid,f2.modify_reason_id,f2.modify_reason_add,f2.uom_freq,f2.code_sx,1,sysdate,0

from {0}.airway_point_double f2

where

f2.significant_point_id=f1.significant_point_id and

f2.significant_point_id={1} and

f2.val_current_state=1

)

where f1.significant_point_id ={1}

", AisLogical.dbuser, pid);

2016.2.13 (年初六) oracle两张表update方法的更多相关文章

  1. Oracle两张表关联批量更新其中一张表的数据

    Oracle两张表关联批量更新其中一张表的数据 方法一(推荐): UPDATE 表2 SET 表2.C = (SELECT B FROM 表1 WHERE 表1.A = 表2.A) WHERE EXI ...

  2. Oracle:同步两张表的相同字段

    有一个需求需要同步两张表的相同字段,比如表A和表B,这两张表是不同的用户下的表,表结构是一样的. 一开始我简单写了一个sql语句,如下: update ord_log1 A set (A.pid, A ...

  3. MySQL实现两张表数据的同步

    有两张表A和B,要求往A里面插入一条记录的同时要向B里面也插入一条记录,向B里面插入一条记录的同时也向A插入一条记录.两张表的结构不同,需要将其中几个字段对应起来.可以用下面的触发器实现. 表A的触发 ...

  4. Oracle 两个表之间更新的实现

    Oracle 两个表之间更新的实现   来源:互联网 作者:佚名 时间:2014-04-23 21:39 Oracle中,如果跨两个表进行更新,Sql语句写成这样,Oracle 不会通过.查了资料,S ...

  5. SQLSERVER中如何快速比较两张表的不一样

    SQLSERVER中如何快速比较两张表的不一样 不知不觉要写2014年的最后一篇博文了~ 一般来说,如何检测两张表的内容是否一致,体现在复制的时候发布端和订阅端的两端的数据上面 我这里罗列了一些如何从 ...

  6. CROSS JOIN连接用于生成两张表的笛卡尔集

    将两张表的情况全部列举出来 结果表: 列= 原表列数相加 行= 原表行数相乘     CROSS JOIN连接用于生成两张表的笛卡尔集. 在sql中cross join的使用: 1.返回的记录数为两个 ...

  7. SQLServer两张表筛选相同数据和不同数据

    概述 项目中经常会对两张数据库表的数据进行比较,选出相同的数据或者不同的数据.在SQL SERVER 2000中只能用Exists来判断,到了SQL SERVER 2005以后可以采用EXCEPT和I ...

  8. django同时查询两张表的数据,合并检索对象返回

    原始需求: 1.一篇文章内容分N个版块,每篇文章的版块数量不同. 2.有个文章搜索功能,需要同时搜索标题和内容. 实现思路: 1.由于每篇文章的内容版块数量不同,因此将每个文章的标题和内容分开存入2张 ...

  9. 关于跨DB增量(增、改)同步两张表的数据小技巧

    有些场景下,需要隔离不同的DB,彼此DB之间不能互相访问,但实际的业务场景又需要从A DB访问B DB的情形,这时怎么办?我认为有如下常规的三种方案: 1.双方提供RESET API,需要访问不同DB ...

随机推荐

  1. jQuery 获取jsp页面中用iframe引入的jsp页面中的值

    <iframe scrolling="no" src="<c:url value='/unitBaseperson/view.do?para=9&op ...

  2. JavaWeb -- Jsp 自定义标签的使用

    Jsp中不要有一行Java代码, 需要的Java代码都要封到自定义标签中. 自定义标签的作用: a.  自定义标签除了可以移除jsp页面java代码外,它也可以实现以上功能. b.  控制jsp页面某 ...

  3. QT XML文档的解析 QXmlStreamReader, DOM,SAX 三种解析方法 简单示例

    0. xml文档如下 <?xml version="1.0"?> <bookindex> <entry term="sidebearings ...

  4. 解决Pycharm中matplotlib画图出错问题(AttributeError: module 'matplotlib' has no attribute 'verbose')

    最近在Linux中使用pycharm过程中使用matplotlib无法画图,总是提示错误 /usr/bin/python3. /home/leo/PycharmProjects/untitled1/E ...

  5. OpenStack网络新项目Dragonflow研究

    https://www.ustack.com/blog/openstack-dragonflow/ 本文由2015年5月30日举行的OpenStack Meetup北京上的演讲整理而成,演讲者为Uni ...

  6. Web API 接口-JavaScript全部api接口文档

    当使用JavaScript编写网页代码时,有很多API可以使用.以下是所有对象.类型等接口的列表,你在开发网页应用程序或站点时使用它们. API文档地址:https://developer.mozil ...

  7. 在OSX上安装python3使用pip安装Flask

    官方的pypi.python.org可能访问不了,可以先将pip配置为豆瓣的pypi镜像 $ mkdir ~/.pip $ vim ~/.pip/pip.conf [global] timeout = ...

  8. C# 6.0 (VS2015 CTP6)

    /* C# 6.0 demo https://github.com/dotnet/roslyn/wiki/Languages-features-in-C%23-6-and-VB-14 */ using ...

  9. hdu 5244 inverse(分治¥)

    inverse Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  10. 利用Pelican搭建个人博客

    博客基于win7系统,python2.7和pelican. 1.安装工具 安装virtualenv pip install virtualenv 下载make,或者make移动至任一目录,并将路径写入 ...