oracle中merge into语句详解(oracle的merge语句)
wptr33 2025-07-24 22:32 6 浏览
由于工作中使用,研究了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'
————————————————
- 上一篇:N张图告诉你K-DB为什么能全面兼容Oracle?
- 已经是最后一篇了
相关推荐
- oracle中merge into语句详解(oracle的merge语句)
-
由于工作中使用,研究了mergeinto语句是insert与update语句的结合,可以同时实现update和insert的功能。一、mergeinto语句的语法。MERGEINTOsch...
- N张图告诉你K-DB为什么能全面兼容Oracle?
-
不是每一款数据库都能全面兼容Oracle,就像不是所有数据库都可以被称之为K-DB。一般数据库能做到的SQL标准和函数上兼容Oracle,而K-DB则能实现更多,在数据库体系架构、集群方式、数据库对象...
- ORACLE 错误代码及解决办法(oracle错误码942)
-
ORA-00001:违反唯一约束条件(.)错误说明:当在唯一索引所对应的列上键入重复值时,会触发此异常。ORA-00017:请求会话以设置跟踪事件ORA-00018:超出最大会话数ORA-00...
- SQL知识大全三):SQL中的字符串处理和条件查询
-
点击上方蓝字关注我们今天是SQL系列的第三讲,我们会讲解条件查询,文本处理,百分比,行数限制,格式化以及子查询。...
- LabVIEW实现Oracle数据库的访问(深入浅出labview数据库应用)
-
1.安装Oracle客户端下载:从Oracle官方网站下载适用于Windows操作系统的Oracle驱动程序。确保下载的版本与LabVIEW环境和操作系统兼容。...
- Oracle查询语句,你知道几个?(oracle常用查询语句)
-
介绍以下非常有用的Oracle查询语句,主要涵盖了日期操作,获取服务器信息,获取执行状态,计算数据库大小等方面的查询。日期/时间查询1、获取当前月份的第一天运行这个命令能快速返回当前月份的第一天,可...
- Oracle数据库中判断字段不为空?(oracle数据库中判断字段不为空的函数)
-
Oracle数据库中如何判断字段不为空在Oracle数据库中,判断字段(列)不为空通常涉及到几种不同的场景和需求。下面是一些常见的方法来检查字段是否不为空:1.使用NVL函数NVL函数可以用来将NU...
- Oracle 字典表使用函数自动转码,自定义函数传参
-
创建函数模板CREATEORREPLACEFUNCTIONdic_val--定义函数(dict_idINVARCHAR2,codeINVARCHAR2)--定义参数RETURN...
- 从上百个字段到1个CLOB:Oracle JSON存储实战指南
-
陆沉盯着左右两个屏幕上显示的数据格式文档,右手小拇指无意思地一下又一下的敲击着机械键盘的Ctrl键,在清脆的“哒哒”声中思考着。...
- 程序员面试中问到的Oracle常用数据类型
-
Oracle中常用数据类型有:1、字符类型1.1、定长字符1.1.1、Char字符长度不够自动在右边加空格符号。最大存2000个字符,当字符长度超出2000个报错。不指定大小默认为1。1.1.2、...
- 了解 Oracle 中单引号与双引号的用法,一篇文章教会你!
-
无论测试或者开发,对数据库的增删改查都是家常便饭。但有些小知识是经常被忽略,却又不能不去了解的,例如单引号和双引号的用法和区别,看完这一篇,你肯定会有收获。...
- Oracle字符串转日期错误,试试TO_TIMESTAMP函数
-
最近,在工作中,发现有些字符串格式无法转换成日期格式,如下图:这种to_date是无法转换的,会报错,因此,需要用到:TO_TIMESTAMP,具体格式如下:TO_TIMESTAMP(字段名,...
- oracle——空字符串('')不能用和!=
-
oracle——空字符串('')不能用<>和!=最近在查询空字符串的数据时发现查询不出数据。后来发现以前的写法在oracle中不能用。记录一下:数据如下:...
- oracle的listagg函数,可以把多行转为一个字符串
-
oracle的listagg函数可以把多行转为一个字符串,用起来很方便,示例如下:witht1as(select'001'asitemcode,'苹果'...
- MySQL 教程的天花板--入门到高级(mysql实用教程)
-
给大家推荐一套MySQL的教程,堪称MySQL教程的天花板。此教程包含...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
git 执行pull错误如何撤销 git pull fail
-
面试官:git pull是哪两个指令的组合?
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
-
- oracle中merge into语句详解(oracle的merge语句)
- N张图告诉你K-DB为什么能全面兼容Oracle?
- ORACLE 错误代码及解决办法(oracle错误码942)
- SQL知识大全三):SQL中的字符串处理和条件查询
- LabVIEW实现Oracle数据库的访问(深入浅出labview数据库应用)
- Oracle查询语句,你知道几个?(oracle常用查询语句)
- Oracle数据库中判断字段不为空?(oracle数据库中判断字段不为空的函数)
- Oracle 字典表使用函数自动转码,自定义函数传参
- 从上百个字段到1个CLOB:Oracle JSON存储实战指南
- 程序员面试中问到的Oracle常用数据类型
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)
- c语言 switch (34)
- git commit (34)