2016.2.13 (年初六) oracle两张表update方法
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方法的更多相关文章
- Oracle两张表关联批量更新其中一张表的数据
Oracle两张表关联批量更新其中一张表的数据 方法一(推荐): UPDATE 表2 SET 表2.C = (SELECT B FROM 表1 WHERE 表1.A = 表2.A) WHERE EXI ...
- Oracle:同步两张表的相同字段
有一个需求需要同步两张表的相同字段,比如表A和表B,这两张表是不同的用户下的表,表结构是一样的. 一开始我简单写了一个sql语句,如下: update ord_log1 A set (A.pid, A ...
- MySQL实现两张表数据的同步
有两张表A和B,要求往A里面插入一条记录的同时要向B里面也插入一条记录,向B里面插入一条记录的同时也向A插入一条记录.两张表的结构不同,需要将其中几个字段对应起来.可以用下面的触发器实现. 表A的触发 ...
- Oracle 两个表之间更新的实现
Oracle 两个表之间更新的实现 来源:互联网 作者:佚名 时间:2014-04-23 21:39 Oracle中,如果跨两个表进行更新,Sql语句写成这样,Oracle 不会通过.查了资料,S ...
- SQLSERVER中如何快速比较两张表的不一样
SQLSERVER中如何快速比较两张表的不一样 不知不觉要写2014年的最后一篇博文了~ 一般来说,如何检测两张表的内容是否一致,体现在复制的时候发布端和订阅端的两端的数据上面 我这里罗列了一些如何从 ...
- CROSS JOIN连接用于生成两张表的笛卡尔集
将两张表的情况全部列举出来 结果表: 列= 原表列数相加 行= 原表行数相乘 CROSS JOIN连接用于生成两张表的笛卡尔集. 在sql中cross join的使用: 1.返回的记录数为两个 ...
- SQLServer两张表筛选相同数据和不同数据
概述 项目中经常会对两张数据库表的数据进行比较,选出相同的数据或者不同的数据.在SQL SERVER 2000中只能用Exists来判断,到了SQL SERVER 2005以后可以采用EXCEPT和I ...
- django同时查询两张表的数据,合并检索对象返回
原始需求: 1.一篇文章内容分N个版块,每篇文章的版块数量不同. 2.有个文章搜索功能,需要同时搜索标题和内容. 实现思路: 1.由于每篇文章的内容版块数量不同,因此将每个文章的标题和内容分开存入2张 ...
- 关于跨DB增量(增、改)同步两张表的数据小技巧
有些场景下,需要隔离不同的DB,彼此DB之间不能互相访问,但实际的业务场景又需要从A DB访问B DB的情形,这时怎么办?我认为有如下常规的三种方案: 1.双方提供RESET API,需要访问不同DB ...
随机推荐
- SSH或者SSM开发web,mysql数据库,数据库配置文件配置不当~数据库读写数据乱码问题解决办法。
相信,大家都有遇到过在传入一个中文string,debug自己的每一行代码时,都发现始终是没有乱码的(即:排除了,源码文件的编码格式是没问题的),但是数据进入数据库之后就是乱掉了. 那么很明显问题就出 ...
- Java -- 利用反射 操作任意数组,包括对象数组 和 基本数据类型的数组
items为任意数组
- 将本地jar包制作成maven依赖
首先,需要配置maven环境,如果没配置,传送门:http://www.cnblogs.com/hyyq/p/6557683.html 然后,需要一个ben地的jar包,这里以阿里云支付宝sdk为例, ...
- PrepareStatement
import java.sql.DriverManager; import java.sql.Connection; import java.sql.PreparedStatement; import ...
- UVA 11827 水
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- nova rebuild
nova rebuild¶ usage: nova rebuild [--rebuild-password <rebuild-password>] [--poll] [--minimal] ...
- 子矩阵(暴搜(全排列)+DP)
子矩阵(暴搜(全排列)+DP) 一.题目 子矩阵 时间限制: 1 Sec 内存限制: 128 MB 提交: 1 解决: 1 [提交][状态][讨论版] 题目描述 给出如下定义: 1. 子矩阵:从一 ...
- FilterDispatcher处理流程
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 前身是:org.apache.struts2.dispatc ...
- 详解 Android 通信
详解 Android 通信 :http://www.androidchina.net/5028.html
- android中shape的属性
<shape> <!– 实心 –> <solid android:color=”#ff9d77″/> <!– 渐变 –> <gradient an ...