[转]oracle update set select from 关联更新
本文转自:http://blog.csdn.net/disiwei1012/article/details/52589181
http://www.blogjava.net/Jhonney/archive/2010/06/25/324503.html
$ sqlplus user/pass SQL*Plus: Release 9.2.0.6. - Production on Wed Aug :: Copyright (c) , , Oracle Corporation. All rights reserved. Connected to:
Oracle9i Enterprise Edition Release 9.2.0.6. - 64bit Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.6. - Production SQL> select * from wwm2; --要更新的表 TOWN ID
-------------------- ---------- ww'jj 111
llll
dddd
lllldf
lllldf
dsafdf ljjjjj
dsafdf TOWN ID
-------------------- ---------- ljjjjj SQL> select * from wwm5; --更新的条件表 TOWN ID
-------------------- ----------
lllldf
test SQL> select wwm2.* from wwm2,wwm5 where wwm2.id=wwm5.id
/ TOWN ID
-------------------- ---------- ww'jj 111
lllldf
lllldf
dsafdf dsafdf rows selected. 所以,每次需要更新8条数据就是正确的. 相信程序员是通过以下类似的SQL更新的,这是错误的,因为没有加WHERE
SQL> update wwm2 set wwm2.town=(select wwm5.town from wwm5 where wwm5.id=wwm2.id)
/ rows updated. SQL> select * from wwm2; TOWN ID
-------------------- ---------- lllldf
lllldf lllldf
lllldf
lllldf
lllldf lllldf TOWN ID
-------------------- ----------
lllldf rows selected. 可以看到13条记录被更新,符合条件的更新正确,不符合条件的也更新为NULL.以下是正确的方法 方法一:
SQL> update wwm2
set town=(select town from wwm5 where wwm5.id=wwm2.id)
where id=(select wwm5.id from wwm5 where wwm5.id=wwm2.id)
/ rows updated. 方法二: 与方法一道理相同,这里需要掌握EXIST的相关用法.
SQL> update wwm2
set town=(select town from wwm5 where wwm5.id=wwm2.id)
where exists (select from wwm5 where wwm5.id=wwm2.id)
rows updated. 方法三:
SQL> update (select a.town atown,a.id aid,b.town btown,b.id bid from wwm2 a,wwm5 b where a.id=b.id)
set atown=btown
/
set atown=btown
*
ERROR at line :
ORA-: cannot modify a column which maps to a non key-preserved table * alter table wwm5 add primary key (id)
SQL> / Table altered. update (select a.town atown,a.id aid,b.town btown,b.id bid from wwm2 a,wwm5 b where a.id=b.id)
* set atown=btown
SQL> / rows updated. 这种方法的局限性就是需要PRIMARY 的支持. 方法四:
1 declare
2 cursor cur_wwm is select town,id from wwm5;
3 begin
4 for my_wwm in cur_wwm loop
5 update wwm2 set town=my_wwm.town
6 where id=my_wwm.id;
7 end loop;
8* end;
SQL> / PL/SQL procedure successfully completed. SQL> select * from wwm2; TOWN ID
-------------------- ---------- lllldf
lllldf
llll
dddd
lllldf
lllldf
lllldf
lllldf
ljjjjj
lllldf TOWN ID
-------------------- ----------
lllldf
ljjjjj 这个方法是最灵活的了. 方法五: 注意,方法五只能适用于WWM5是WWM2的子集的时候.
merge into wwm2
using (select town,id from wwm5) b
on (wwm2.id=b.id)
when matched then update set town=b.town
* when not matched then insert (town,id) values (null,null)
SQL> / rows merged. SQL> select * from wwm2; TOWN ID
-------------------- ----------
---注意这个地方,被插入了一个空值.因为WWM5的ID=9984在WWM2中不能匹配,根本原因是ORACLE9必须有WHEN NOT MATCHED子句,但是ORACLE10可以不许要,也就是ORACLE10可以不写WHEN NOT MATCHED ,就不必插入NULL值了,为解决这个问题,下一步会DELETE WWM5的ID=,这样一来就不会执行WHEN NOT MATCHED lllldf
lllldf
llll
dddd
lllldf
lllldf
lllldf
lllldf
ljjjjj TOWN ID
-------------------- ----------
lllldf
lllldf
ljjjjj rows selected. SQL> delete from wwm5 where id=; row deleted. SQL> merge into wwm2
SQL> using (select town,id from wwm5) b
SQL> on (wwm2.id=b.id)
SQL> when matched then update set town=b.town
SQL> * when not matched then insert (town,id) values (null,null)
SQL> / rows merged. 以上就是5种关连更新的例子了,希望能给开发人员解惑. 说明:如果select 子句可以返回多行记录,但返回适合where条件的记录只能是唯一的,否则将会报返回单行的select子句返回多行的错误,因为update只能跟据此处的where子句(内层where)进行相应记录的匹配更新,一次只能是一条。
[转]oracle update set select from 关联更新的更多相关文章
- oracle update set select from 关联更新
工作中有个需求,现在新表中有一些数据跟老表的基本一样,这样只需要把老表中数据搬到新表中就可以了,同时把不同的字段修改下数据即可,在修改字段时发现,需要指定一个条件,比如主键id,来修改某条记录,这样一 ...
- oracle学习笔记:update一整列 关联更新
普通的 update 都是根据条件来对部分列的内容进行修改,用法如下: update temp_cwh_table set name = 'xxx' where id = 1; 假设现在有2张表:A. ...
- Oracle update和select 关联
Oracle update和select 关联 目录 Oracle update和select 关联 1.介绍 2.解决方法 2.1.需求 2.2.错误演示 2.3.解决方法 1.介绍 本文主要向大家 ...
- Update和Select结合统计更新
Update和Select结合统计更新 update table_a set updatetime=getdate(), name=b.name from (select name,age from ...
- Oracle update时做表关联
感觉还是sqlserver中的写法比较好理解,Oracle的写法都快把我搞晕了, 注意: 1.要修改的表,不要加入到子查询中,用别名在子查询中与其他表进行关联即可. 2.exsits不能少,exsit ...
- Oracle\MS SQL Server Update多表关联更新
原文:Oracle\MS SQL Server Update多表关联更新 一条Update更新语句是不能更新多张表的,除非使用触发器隐含更新.而表的更新操作中,在很多情况下需要在表达式中引用要更新的表 ...
- 转 update关联更新在sqlserver和oracle中的实现
sqlserver和oracle中实现update关联更新的语法不同,都可以通过inline view(内嵌视图)来实现,总的来说sqlserver更简单些. 测试例子如下: create table ...
- Oracle Update 语句语法与性能分析 - 多表关联
Oracle Update 语句语法与性能分析 - 多表关联 为了方便起见,建立了以下简单模型,和构造了部分测试数据: 在某个业务受理子系统BSS中, SQL 代码 --客户资料表 create ...
- Oracle SQL性能优化 - 根据大表关联更新小表
需求: 小表数据量20w条左右,大表数据量在4kw条左右,需要根据大表筛选出150w条左右的数据并关联更新小表中5k左右的数据. 性能问题: 对筛选条件中涉及的字段加index后,如下常规的updat ...
随机推荐
- 如何在VMware Workstation11的Windows Server 2008 R2中安装XAMPP?
我在VMware Workstation11的Windows Server 2008 R2打算安装XAMPP,但是总是有问题,经过两天的不懈努力,终于实现了,下面我具体说一说我遇到的问题和解决方法! ...
- leetcode 39 组合总和 JAVA
题目: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制 ...
- TOMCAT在POST方法提交参数丢失问题
最近写程序发现post参数接收不到了,jdk,eclipse,jar包,换了多次都不行,后来看到网上一篇文章: 随后设置Tomcat中POST方式提交数据大小: maxPostSize="0 ...
- 多线程DP
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- vue.js 知识点(四)
看完了vue.js的官方文档,大概对这些知识有了那么一点的了解了,但是很多具体的运用还不太清楚,现在就总结一下,关于其中的一些知识点的运用: v-bind: 动态绑定指令,默认情况下,是给html ...
- JMeter—监听器
用来显示JMeter取样器的测试结果,能够以树.表.图形形式显示,也可以以文件方式保存. 一.设置默认配置 初始化配置文件设置: 监听器默认保存哪些数据域,可以在jmeter.properties(或 ...
- modalTransitionStyle各种present效果
coverVertical(默认的) flipHorizontal crossDissolve partialCurl
- web 应用的部署
一.项目管理 : zentao(国产开源),其他 project.redmine.trac 二.自动部署: jenkins:自动化配置 docker:容器,类似虚拟机,不过只是本机系统的内核的一个虚拟 ...
- 关于web界面设计的整体可维护性的感悟
1.表现与数据分开管理: 某些数据具备特殊的表现格式,比如颜色,大小等等.为了对这些格式表现分开管理进行 a.使用css定义该类型数据的表现形式: 定义数据的类别,通过该类别对数据格式进行统一定义 . ...
- 搭建gogs
https://blog.csdn.net/hwm_life/article/details/82969005 服务器环境 CentOS 7 64位 安装Gogs所需的其它环境 需要安装的依赖有Ngi ...