Oracle merge into介绍
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Oracle merge into介绍,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1941字,纯文字阅读大概需要3分钟。
内容图文
?
在Oracle数据库常用操作中,有的时候需要使用B表来更新A表中的内容,一种方法使用update关联两表进行更新,还有一种办法即使用merge into。
?
merge into 一条语句可以实现update和insert的功能,并且效率较高
语法
?
MERGE INTO [your table-name] [rename your table here]
USING ( [write your query here] )[rename your query-sql and using just like a table]
ON ([conditional expression here] AND [...]...)
WHEN MATHED THEN [here you can execute some update sql or something else ]
WHEN NOT MATHED THEN [execute something else here ! ]
?
?
举例?
建立表test_sj_salary,插入示例数据
?
create table test_sj_salary ( id integer primary key, name varchar2(100), salary integer ); truncate table test_sj_salary; insert into test_sj_salary (ID, NAME, SALARY) values (1, ‘张三‘, 5000); insert into test_sj_salary (ID, NAME, SALARY) values (2, ‘李四‘, 4000); insert into test_sj_salary (ID, NAME, SALARY) values (3, ‘王五‘, 6000); insert into test_sj_salary (ID, NAME, SALARY) values (4, ‘李六‘, 3500); commit;
?
建立test_sj_cp,插入示例数据
create table test_sj_cp ( id integer primary key, name varchar2(100), salary integer ); truncate table test_sj_cp; insert into test_sj_cp (ID, NAME, SALARY) values (9, ‘张三‘, 9000); insert into test_sj_cp (ID, NAME, SALARY) values (10, ‘李四‘, 10000); insert into test_sj_cp (ID, NAME, SALARY) values (11, ‘王二麻子‘, 11000); commit;
?
需要实现的功能:使用test_sj_cp根据相同name更新test_sj_salary中的salary字段
?
方法一,采用普通的update方法
?
update test_sj_salary a set a.salary=(select b.salary from test_sj_cp b where b.name=a.name) where exists (select 1 from test_sj_cp c where c.name=a.name); commit;
?
方法二,采用merge into方法
merge into test_sj_salary a using test_sj_cp b on (a.name=b.name) when matched then update set a.salary=b.salary; commit;
?是不是很简洁!
?
扩展功能,通过merge实现匹配到的更新,匹配不到的插入数据
?
merge into test_sj_salary a using test_sj_cp b on (a.name=b.name) when matched then update set a.salary=b.salary when not matched then insert(id,name,salary) values(b.id,b.name,b.salary); commit;
?
?
?
?
?
原文:http://wodeguozili.iteye.com/blog/2304081
内容总结
以上是互联网集市为您收集整理的Oracle merge into介绍全部内容,希望文章能够帮你解决Oracle merge into介绍所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。