您好,欢迎访问一九零五行业门户网

oracle中merge into的用法是什么

在oracle中,“merge into”用于更新表中的数据,可以将一个表中的数据插入另一个表中,若被插入的表中已经有该数据则更新该数据,若没有该数据则会在被插入的表中新增数据。
本教程操作环境:windows10系统、oracle 11g版、dell g3电脑。
oracle中merge into的用法是什么oracle9g引入了merge命令,你能够在一个sql语句中对一个表同时执行inserts和update操作. merge命令从一个或多个数据源中选择行来update或insert到一个或多个表.在oracle 10g中merge有如下一些改进:
1、update或insert子句是可选的
2、update和insert子句可以加where子句
3、在on条件中使用常量过滤谓词来insert所有的行到目标表中,不需要连接源表和目标表
4、update子句后面可以跟delete子句来去除一些不需要的行
5、源表就是using关键字后面跟的表,目标表就是将要被merge into的表
6、merge into 中所有的update、insert、delete都是针对目标表来操作的。由于merge into已经制定了操作的表,所以update、insert、delete都不需要再显示指出表名
7、总之,merge into的作用就是 解决用b表跟新a表数据,如果a表中没有,则把b表的数据插入a表或向一个表中插入数据,如果该表已有该数据则更新,反之新增数据。
语法:
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_one
create table test_one(  id   number not null    primary key,  name varchar2(255),  ip   varchar2(255),  memo varchar2(255))commit;
随便添加几条数据作为测试数据
insert into test_one (id, name, ip, memo) values (1, '2', '3', '周文军');insert into test_one (id, name, ip, memo) values (2, '66', '366', '2656');insert into test_one (id, name, ip, memo) values (3, '5656', '626', '2626');insert into test_one (id, name, ip, memo) values (4, '5656', '2626', '626');
好了,我们的数据表已经建成,如下图:
如果我们需要新增一条数据,一般会这样进行
insert into test_one (id, name, ip, memo) values (5, 'mrhu', '127.0.0.1.0', '王先生的ip');
但我们希望可以先用id进行判断,没有该数据新增,有该数据更新,怎么实现呢?
那么merge into命令来了,直接撸代码:
merge into test_one mtb using (select '5' as id, 'mrhu' as name,'127.0.0.1.0' as ip,'王先生的ip' as memo from dual)mmb on (mtb.id = mmb.id)when matched thenupdate set mtb.name = mmb.name,mtb.ip = mmb.ip,mtb.memo=mmb.memowhen not matched theninsert (mtb.id, mtb.name,mtb.ip,mtb.memo) values(mmb.id,mmb.name,mmb.ip,mmb.memo);
运行如下:
我们再来看看表中数据:
数据新增成功!
那么我们如何测试更新呢?很简单,我们将memo=‘王先生的ip’ 改为 memo=‘本大美女的ip’来进行测试
merge into test_one mtb using (select '5' as id, 'mrhu' as name,'127.0.0.1.0' as ip,'本大美女的ip' as memo from dual)mmb on (mtb.id = mmb.id)when matched thenupdate set mtb.name = mmb.name,mtb.ip = mmb.ip,mtb.memo=mmb.memowhen not matched theninsert (mtb.id, mtb.name,mtb.ip,mtb.memo) values(mmb.id,mmb.name,mmb.ip,mmb.memo);
运行如下:
我们再来看看表中数据:
数据更新成功啦!
需求二:将a表中的数据添加到b表中,要求通过主键来进行判断,如果包含该数据则更新,反之新增我们再创建一个表test_two作为表b,test_one作为表a
create table tes(  id   number not null    primary key,  code varchar2(255),  memo varchar2(255));commit;
好了 表test_two建立好了,我们先来添加一条数据吧!
insert into root.test_two (id, code, memo) values (5, 'mrhu', '隔壁老王的ip');
我们再看看test_two中的数据:
我们来将test_one中的数据导到我们新建的表中,通过分析,我们发现,test_two 表中有了一条数据,id为5,test_one中也有一条id为5的数据,预期执行效果为test_twoid为5的数据的memo字段值将更新为test_one中的‘本大美女的ip’,其他值进行新增操作。
我们写代码来验证一下:
merge into test_two mtb using (select id,name,ip,memo from test_one) mmb on (mtb.id = mmb.id)when matched thenupdate set mtb.code = mmb.name,mtb.memo = mmb.memowhen not matched theninsert (mtb.id,mtb.code,mtb.memo) values (mmb.id,mmb.name,mmb.memo);
我们来看看效果:
执行结果与预期结果一致,好了,merge into的用法你们学会了吗! 喜欢的关注支持一下!
推荐教程:《oracle视频教程》
以上就是oracle中merge into的用法是什么的详细内容。
其它类似信息

推荐信息