Oracle中merge into的用法

我们操作数据库的时候,有时候会遇到insertOrUpdate这种需求。

如果数据库中存在数据就update,如果不存在就insert。

以前的时候,需要额外select查询一下,如果有数据就update,如果没有数据就insert。

而现在Orcale数据库都提供了 MERGE 方法来处理这种需求。

MERGE 命令使用一条语句从一个或者多个数据源中完成对表的更新和插入数据。

MERGE 语法:

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 MATCHED THEN [here you can execute some update sql or something else ]
WHEN NOT MATCHED THEN [execute something else here ! ] 

这里我们创建两张学生信息表,以此为例来进行说明:

-- Create table
create table STUDENT_1
(
  id               VARCHAR2(64) not null,
  code             VARCHAR2(30) not null,
  name             VARCHAR2(100) not null,
  age              NUMBER(3) not null,
  created_by       VARCHAR2(64) not null,
  creation_date    DATE not null,
  last_updated_by  VARCHAR2(64) not null,
  last_update_date DATE not null,
  last_update_ip   VARCHAR2(64) not null,
  version          NUMBER(16) not null
)
tablespace MES614
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 16
    next 8
    minextents 1
    maxextents unlimited
  );
-- Add comments to the table
comment on table STUDENT_1
  is ‘1班学生信息表‘;
-- Add comments to the columns
comment on column STUDENT_1.id
  is ‘ID‘;
comment on column STUDENT_1.code
  is ‘学生证号‘;
comment on column STUDENT_1.name
  is ‘学生姓名‘;
comment on column STUDENT_1.age
  is ‘年龄‘;
comment on column STUDENT_1.created_by
  is ‘创建人‘;
comment on column STUDENT_1.creation_date
  is ‘创建日期‘;
comment on column STUDENT_1.last_updated_by
  is ‘最后更新人‘;
comment on column STUDENT_1.last_update_date
  is ‘最后更新时间‘;
comment on column STUDENT_1.last_update_ip
  is ‘最后更新IP‘;
comment on column STUDENT_1.version
  is ‘版本‘;
-- Create/Recreate primary, unique and foreign key constraints
alter table STUDENT_1
  add constraint PK_STUDENT_1 primary key (ID)
  using index
  tablespace MES614
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );

学生信息表2除了和学生信息表1名称不一样之外,结构完全一致,所以这里就不在把学生信息表2(student_2)的建表语句放上来了。(Mes614是表空间名)

我们先手动的在student_2表中插入一些数据,然后先单独的写两个sql,分别将student_2中的数据插入或者更新到student_1中:

--将student_2中的数据插入到student_1中
insert into student_1
  (id,
   code,
   name,
   age,
   created_by,
   creation_date,
   last_updated_by,
   last_update_date,
   last_update_ip,
   version)
  select t.id,
         t.code,
         t.name,
         t.age,
         t.created_by,
         t.creation_date,
         t.last_updated_by,
         t.last_update_date,
         t.last_update_ip,
         t.version
    from student_2 t
    --where ...

--将student_1中数据除ID外随便乱改,然后通过merge into sql将其更新回来
merge into student_1 t
using (select t1.id,
              t1.code,
              t1.name,
              t1.age
         from student_2 t1) me --这里可以只查出需要的字段
on (t.id = me.id)
when matched then  --当on()中条件成立时执行
  update
     set t.code = me.code,
         t.name = me.name,
         t.age  = me.age,
         t.version = t.version + 1;

下面我们就用merge into sql来同时进行插入和更新操作:

merge into student_1 t
using (select t1.id,
              t1.code,
              t1.name,
              t1.age,
              t1.created_by,
              t1.creation_date,
              t1.last_updated_by,
              t1.last_update_date,
              t1.last_update_ip,
              t1.version
         from student_2 t1) me --这里可以只查出需要的字段
on (t.id = me.id)
when matched then  --当on()中条件成立时执行
  update
     set t.code = me.code,
         t.name = me.name,
         t.age  = me.age,
         t.version = t.version + 1
when not matched then  --当on()中条件不成立时执行这部分
  insert
    (id,
     code,
     name,
     age,
     created_by,
     creation_date,
     last_updated_by,
     last_update_date,
     last_update_ip,
     version)
  values
    (me.id,
     me.code,
     me.name,
     ‘22‘,
     me.created_by,
     me.creation_date,
     me.last_updated_by,
     me.last_update_date,
     me.last_update_ip,
     me.version);

注意事项:

Merge Into的原理是,从using 搜出来的结果逐条与on条件匹配,然后决定是update还是Insert。 当USING后面的sql没有查询到数据的时候,Merge Into语句是不会执行update和Insert操作的。

所以要想让Merge Into正常运行,要保证USING 后面的SELECT有数据。

原文地址:https://www.cnblogs.com/1012hq/p/11364698.html

时间: 2024-08-29 06:14:48

Oracle中merge into的用法的相关文章

oracle中 merge into 的用法

很多时候我们需要通过筛选条件同时对表进行 更新,插入,删除 等操作.这样如果我们单一的去操作表会显得很麻烦,下面会说到这个merge  into 的用法会极大的优化我们操作表的时间和代码量. 举例,先新建2个表: create table book( id number, name varchar(64), price number, primary key(id) ) create table pbook as select * from book delete pbook 这里create

Oracle 中MERGE语句的用法(转载)

原文章出处(http://blog.csdn.net/lichkui/article/details/4306299) MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句.通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询,连接条件匹配上的进行UPDATE,无法匹配的执行INSERT.这个语法仅需要一次全表扫描就完成了全部工作,执行效率要高于INSERT+UPDATE. //表1create table subs(msid number(9), 

Oracle中Merge into用法总结 (转载)

Oracle中Merge into用法总结 (出处:http://www.cnblogs.com/dongsheng/p/4384754.html) 起因: 前段时间,因为涉及到一张表的大数据操作,要同时进行增删改,我跟师傅想了很多优化办法,结果都不尽人意.刚开始用的就是原始算法,先更新现有记录,再插入满足要求的其他记录,最后再删除多余记录,但是少量数据还可以,10W条数据就不行了,前台的超时时间是60s,远远无法满足要求.之后又想办法将任务进行拆分,根据每条记录流水号尾字符不同进行拆分,用多个

Oracle中merge into的使用 (转)

http://blog.csdn.net/yuzhic/article/details/1896878 http://blog.csdn.net/macle2010/article/details/5980965 该命令使用一条语句从一个或者多个数据源中完成对表的更新和插入数据. ORACLE 9i 中,使用此命令必须同时指定UPDATE 和INSERT 关键词,ORACLE 10g 做了如下改动. 1,insert 和update是可选的 2,UPDATE 和INSERT 后面可以跟WHERE

Oracle中merge into的使用

http://blog.csdn.net/yuzhic/article/details/1896878 http://blog.csdn.net/macle2010/article/details/5980965 该命令使用一条语句从一个或者多个数据源中完成对表的更新和插入数据. ORACLE 9i 中,使用此命令必须同时指定UPDATE 和INSERT 关键词,ORACLE 10g 做了如下改动. 1,insert 和update是可选的 2,UPDATE 和INSERT 后面可以跟WHERE

Oracle中常用的to_Char用法详解

Oracle中常用的to_Char用法详解(有FMT的详细列表) The following are number examples for the to_char function. to_char(1210.73, '9999.9') would return '1210.7' to_char(1210.73, '9,999.99') would return '1,210.73' to_char(1210.73, '$9,999.00') would return '$1,210.73'

oracle 中start with 的用法

oracle 中start with 的用法 发表于2013/3/3 23:29:35  40191人阅读 分类: Oracle oracle 提供了start with connect by 语法结构可以实现递归查询. 1. 一个简单举例: SQL> select *  from test; BILL_MONTH           DAY_NUMBER MSISDN -------------------- ---------- -------------------- 200803    

oracle中insert all的用法

oracle中insert all的用法 现在有个需求:将数据插入多个表中.怎么做呢?可以使用insert into语句进行分别插入,但是在oracle中有一个更好的实现方式:使用insert all语句. insert all语句是oracle中用于批量写数据的 .insert all分又为无条件插入和有条件插入. 一.表和数据准备 --创建表 CREATE TABLE stu( ID NUMBER(3), NAME VARCHAR2(30), sex VARCHAR2(2) ); --删除表

Oracle中Merge into用法总结

MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句.通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询,连接条件匹配上的进行UPDATE,无法匹配的执行INSERT.这个语法仅需要一次全表扫描就完成了全部工作,执行效率要高于INSERT+UPDATE. 语法: MERGE INTO [your table-name] [rename your table here] USING ( [write your query here] )[rename