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

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

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

由于工作中使用,研究了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'

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

相关推荐

Python字符串终极指南!单引号、双引号、三引号区别全解析

导语:Python中字符串(str)是最核心的数据类型!无论你是输出"HelloWorld"还是处理用户数据,都离不开它。今天彻底讲清字符串的三大定义方式及其核心区别,新手必看!...

python 字符串的定义和表示_python字符串的用法

在Python中,字符串是一序列字符的集合。定义一个字符串可以使用单引号或双引号括起来的字符序列。...

简单的python-熟悉字符串相关的操作

str.py:#-*-coding:utf-8-*-#测试函数deff():#字符串使用单引号定义s1='test'print(s...

Python初学者:3招搞定长字符串逐行读取,代码超简单

刚学Python的小伙伴,是不是遇到过这种尴尬情况?拿到一段老长的多行字符串——比如从文档里复制的日志、一段带换行的文章,想一行一行处理,如果直接打印全堆在一起,手动切又怕漏行,咋整啊?别慌!今天就给...

Python 字符串_python字符串型怎么表达

除了数字,Python还可以操作字符串。字符串的形式是单引号('......')双引号(''.........'')或三个单引号(''&...

贴身口语第二关:请求帮忙、道歉、指路、接受礼物

02-@askforhelp请求协助1.F:Excuseme.Canyouhelpme?M:Yes,whatcanIdoforyou?...

NBA赛季盘点之九大装逼&炫技时刻:“歪嘴战神”希罗领衔

欢迎大家来到直播吧NBA赛季盘点,历经许多波折,2019-20赛季耗时整整一年才圆满收官。魔幻的一年里有太多的时刻值得我们去铭记,赛场上更是不乏球员们炫技与宣泄情绪的装逼时刻,本期盘点就让我们来回顾一...

一手TTS-2语音合成模型安装教程及实际使用

语音合成正从云端调用走向本地部署,TTS-2模型作为开源语音生成方案之一,正在被越来越多开发者尝试落地。本篇文章从环境配置到推理调用,详尽拆解TTS-2的安装流程与使用技巧,为语音产品开发者提供...

网友晒出身边的巨人 普通人站一旁秒变“霍比特人”

当巨人遇到霍比特人,结果就是“最萌身高差”。近日网友们晒出了身边的巨人,和他们站在一起,普通人都变成了“霍比特人”。CanYouTellWho'sRelated?TheDutchGiant...

分手后我们还能做朋友吗?_分手后我们还能做朋友吗

Fewrelationshipquestionsareaspolarizingaswhetherornotyoushouldstayfriendswithanex.A...

如何用C语言实现Shellcode Loader

0x01前言之前github找了一个基于go的loader,生成后文件大小6M多,而且细节不够了解,一旦被杀,都不知道改哪里,想来还是要自己写一个loader...

微星Z490如何装Windows10系统以及怎么设 BIOS

小晨儿今天给大家讲一下msi微星Z490重怎样装系统以及怎么设置BIOS。一、安装前的准备工作1、一、安装前的准备工作1、备份硬盘所有重要的文件(注:GPT分区转化MBR分区时数据会丢失)2...

超实用!互联网软件开发人员不可不知的 Git 常用操作命令

在互联网软件开发的协作场景中,Git是不可或缺的版本控制工具。掌握其核心命令,能让代码管理效率大幅提升。本文精选Git高频实用命令,结合场景化说明,助你快速上手。仓库初始化与克隆...

AI项目的持续集成持续部署实践_ai 项目

在独立开发AI工具的过程中,笔者逐步实践了一套高效的软件项目持续集成与持续部署(CI/CD)流程。这套流程以Git、GitHub和Vercel为核心,实现了从代码提交到生产环境上线的全链路自动化。这篇...

总结几个常用的Git命令的使用方法

1、Git的使用越来越广泛现在很多的公司或者机构都在使用Git进行项目和代码的托管,Git有它自身的优势,很多人也喜欢使用Git。...