百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT技术 > 正文

oracle中merge into语句详解(oracle的merge语句)

wptr33 2025-07-24 22:32 61 浏览

由于工作中使用,研究了merge into 语句是insert 与update语句的结合,可以同时实现update和insert的功能。

一、merge into语句的语法。

MERGE INTO schema. table alias

USING { schema. table | views | query} alias

ON {(condition) }

WHEN MATCHED THEN

UPDATE SET {clause}

WHEN NOT MATCHED THEN

INSERT VALUES {clause};

--解析

INTO 子句

用于指定你所update或者Insert目的表。

USING 子句

用于指定你要update或者Insert的记录的来源,它可能是一个表,视图,子查询。

ON Clause

用于目的表和源表(视图,子查询)的关联,如果匹配(或存在),则更新,否则插入。

merge_update_clause

用于写update语句

merge_insert_clause

用于写insert语句

二、merge 语句的各种用法练习

创建表及插入记录

create table t_B_info_aa

( id varchar2(20),

name varchar2(50),

type varchar2(30),

price number

);

insert into t_B_info_aa values('01','冰箱','家电',2000);

insert into t_B_info_aa values('02','洗衣机','家电',1500);

insert into t_B_info_aa values('03','热水器','家电',1000);

insert into t_B_info_aa values('04','净水机','家电',1450);

insert into t_B_info_aa values('05','燃气灶','家电',800);

insert into t_B_info_aa values('06','太阳能','家电',1200);

insert into t_B_info_aa values('07','西红柿','食品',1.5);

insert into t_B_info_aa values('08','黄瓜','食品',3);

insert into t_B_info_aa values('09','菠菜','食品',4);

insert into t_B_info_aa values('10','香菇','食品',9);

insert into t_B_info_aa values('11','韭菜','食品',2);

insert into t_B_info_aa values('12','白菜','食品',1.2);

insert into t_B_info_aa values('13','芹菜','食品',2.1);

create table t_B_info_bb

( id varchar2(20),

type varchar2(50),

price number

);

insert into t_B_info_bb values('01','家电',2000);

insert into t_B_info_bb values('02','家电',1000);

1) update和insert同时使用

merge into t_B_info_bb b

using t_B_info_aa a --如果是子查询要用括号括起来

on (a.id = b.id and a.type = b.type) --关联条件要用括号括起来

when matched then

update set b.price = a.price --update 后面直接跟set语句

when not matched then

insert (id, type, price) values (a.id, a.type, a.price) --insert 后面不加into

---这条语句根据t_B_info_aa 更新了t_B_info_bb中的一条记录,插入了11条记录

2)只插入不更新

--处理表中数据,使表中的一条数据发生变化,删掉一部分数据。用来验证只插入不更新的功能

update t_B_info_bb b set b.price=1000 where b.id='02';

delete from t_B_info_bb b where b.type='食品';

--只是去掉了when matched then update语句

merge into t_B_info_bb b

using t_B_info_aa a

on (a.id = b.id and a.type = b.type)

when not matched then

insert (id, type, price) values (a.id, a.type, a.price)

3)只更新不插入

--处理表中数据,删掉一部分数据,用来验证只更新不插入

delete from t_B_info_bb b where b.type='食品';

--只更新不插入,只是去掉了when not matched then insert语句

merge into t_B_info_bb b

using t_B_info_aa a

on (a.id = b.id and a.type = b.type)

when matched then

update set b.price = a.price

三、加入限制条件的操作

变更表中数据,便于练习

update t_B_info_bb b

set b.price = 1000

where b.id='02'


delete from t_b_info_bb b where b.id not in ('01','02') and b.type='家电'


update t_B_info_bb b

set b.price = 8

where b.id='10'


delete from t_b_info_bb b where b.id in ('11','12','13') and b.type='食品'

表中数据

执行merge语句:脚本一和脚本二执行结果相同

脚本一: merge into t_B_info_bb b

using t_B_info_aa a

on (a.id = b.id and a.type = b.type)

when matched then

update set b.price = a.price where a.type = '家电'

when not matched then

insert

(id, type, price)

values

(a.id, a.type, a.price) where a.type = '家电';


----------------------

脚本二: merge into t_B_info_bb b

using (select a.id, a.type, a.price

from t_B_info_aa a

where a.type = '家电') a

on (a.id = b.id and a.type = b.type)

when matched then

update set b.price = a.price

when not matched then

insert (id, type, price) values (a.id, a.type, a.price);

结果:

上面两个语句是只对类型是家电的语句进行插入和更新

而脚本三是只对家电进行更新,其余的全部进行插入

merge into t_B_info_bb b

using t_B_info_aa a

on (a.id = b.id and a.type = b.type)

when matched then

update set b.price = a.price

where a.type = '家电'

when not matched then

insert

(id, type, price)

values

(a.id, a.type, a.price);

四、加删除操作

update子句后面可以跟delete子句来去掉一些不需要的行

delete只能和update配合,从而达到删除满足where条件的子句的记录

例句:

merge into t_B_info_bb b

using t_B_info_aa a

on (a.id=b.id and a.type = b.type)

when matched then

update set b.price=a.price

delete where (a.type='食品')

when not matched then

insert values (a.id, a.type, a.price) where a.id = '10'

————————————————

相关推荐

oracle数据导入导出_oracle数据导入导出工具

关于oracle的数据导入导出,这个功能的使用场景,一般是换服务环境,把原先的oracle数据导入到另外一台oracle数据库,或者导出备份使用。只不过oracle的导入导出命令不好记忆,稍稍有点复杂...

继续学习Python中的while true/break语句

上次讲到if语句的用法,大家在微信公众号问了小编很多问题,那么小编在这几种解决一下,1.else和elif是子模块,不能单独使用2.一个if语句中可以包括很多个elif语句,但结尾只能有一个...

python continue和break的区别_python中break语句和continue语句的区别

python中循环语句经常会使用continue和break,那么这2者的区别是?continue是跳出本次循环,进行下一次循环;break是跳出整个循环;例如:...

简单学Python——关键字6——break和continue

Python退出循环,有break语句和continue语句两种实现方式。break语句和continue语句的区别:break语句作用是终止循环。continue语句作用是跳出本轮循环,继续下一次循...

2-1,0基础学Python之 break退出循环、 continue继续循环 多重循

用for循环或者while循环时,如果要在循环体内直接退出循环,可以使用break语句。比如计算1至100的整数和,我们用while来实现:sum=0x=1whileTrue...

Python 中 break 和 continue 傻傻分不清

大家好啊,我是大田。...

python中的流程控制语句:continue、break 和 return使用方法

Python中,continue、break和return是控制流程的关键语句,用于在循环或函数中提前退出或跳过某些操作。它们的用途和区别如下:1.continue(跳过当前循环的剩余部分,进...

L017:continue和break - 教程文案

continue和break在Python中,continue和break是用于控制循环(如for和while)执行流程的关键字,它们的作用如下:1.continue:跳过当前迭代,...

作为前端开发者,你都经历过怎样的面试?

已经裸辞1个月了,最近开始投简历找工作,遇到各种各样的面试,今天分享一下。其实在职的时候也做过面试官,面试官时,感觉自己问的问题很难区分候选人的能力,最好的办法就是看看候选人的github上的代码仓库...

面试被问 const 是否不可变?这样回答才显功底

作为前端开发者,我在学习ES6特性时,总被const的"善变"搞得一头雾水——为什么用const声明的数组还能push元素?为什么基本类型赋值就会报错?直到翻遍MDN文档、对着内存图反...

2023金九银十必看前端面试题!2w字精品!

导文2023金九银十必看前端面试题!金九银十黄金期来了想要跳槽的小伙伴快来看啊CSS1.请解释CSS的盒模型是什么,并描述其组成部分。...

前端面试总结_前端面试题整理

记得当时大二的时候,看到实验室的学长学姐忙于各种春招,有些收获了大厂offer,有些还在苦苦面试,其实那时候的心里还蛮忐忑的,不知道自己大三的时候会是什么样的一个水平,所以从19年的寒假放完,大二下学...

由浅入深,66条JavaScript面试知识点(七)

作者:JakeZhang转发链接:https://juejin.im/post/5ef8377f6fb9a07e693a6061目录...

2024前端面试真题之—VUE篇_前端面试题vue2020及答案

添加图片注释,不超过140字(可选)...

今年最常见的前端面试题,你会做几道?

在面试或招聘前端开发人员时,期望、现实和需求之间总是存在着巨大差距。面试其实是一个交流想法的地方,挑战人们的思考方式,并客观地分析给定的问题。可以通过面试了解人们如何做出决策,了解一个人对技术和解决问...