时间:2021-07-01 10:21:17 帮助过:1人阅读
merge语句语法如下:
merge into table_name t
using (subquery) s on (s.column = t.column)
when matched then update ...
when not matched then insert ...
on关键字声明了关联条件,当有记录匹配时执行Update语句,没有匹配时,执行Insert语句。
创建一张student表:
create table student(
sno varchar2(3) not null, -- 学生编号
sname varchar(9) not null, -- 学生姓名
ssex char(2) not null -- 性别
);
?
表已创建。
插入一条学生标号为001的数据:
SQL> merge into student s
using (select ‘001‘ sno,‘KangKang‘ sname,‘M‘ ssex from dual) t
on (s.sno = t.sno)
when matched then
update set s.sname = t.sname,s.ssex = t.ssex
when not matched then
insert (s.sno,s.sname,s.ssex) values(t.sno,t.sname,t.ssex);
?
1 行已合并。
?
SQL> select * from student;
?
SNO SNAME SS
--- --------- --
001 KangKang M
再次插入一条学生编号为001的数据,不过sname和ssex不一样:
SQL> merge into student s
using (select ‘001‘ sno,‘Maria‘ sname,‘F‘ ssex from dual) t
on (s.sno = t.sno)
when matched then
update set s.sname = t.sname,s.ssex = t.ssex
when not matched then
insert (s.sno,s.sname,s.ssex) values(t.sno,t.sname,t.ssex);
1 行已合并。
SQL> select * from student;
SNO SNAME SS
--- --------- --
001 Maria F
Oracle Merge语句
标签:null sql nta -- table 没有 插入 upd ade