oracle中merge into简单实例

时间:2022-05-15 05:19:06

merge info用法,简单总计就是“有则更新,无则插入”


1、创建学生表
create table  t_B_info_student   (
  s_no varchar2(10),
  s_name varchar2(100),
  s_sex varchar2(10),
  create_by varchar2(10),
  create_date date
); 

2、添加学生信息
begin
  for i in 1 .. 10 loop
    insert into  t_B_info_student
      (s_no, s_name, s_sex, create_by, create_date)
    values
      ('N' || lpad(i, 9, 0), '姓名' || i,decode(mod(i,2),0,'男','女') , 'admin', sysdate);
  end loop;
end ;  

3、查询学生信息
select * from t_B_info_student ; 
  S_NO S_NAME S_SEX CREATE_BY CREATE_DATE
1 N000000001 姓名1 admin 2016/11/11 10:17:53
2 N000000002 姓名2 admin 2016/11/11 10:17:53
3 N000000003 姓名3 admin 2016/11/11 10:17:53
4 N000000004 姓名4 admin 2016/11/11 10:17:53
5 N000000005 姓名5 admin 2016/11/11 10:17:53
6 N000000006 姓名6 admin 2016/11/11 10:17:53
7 N000000007 姓名7 admin 2016/11/11 10:17:53
8 N000000008 姓名8 admin 2016/11/11 10:17:53
9 N000000009 姓名9 admin 2016/11/11 10:17:53
10 N000000010 姓名10 admin 2016/11/11 10:17:53


4、创建学生老师对照表
create table t_B_comp_student_teacher(
  s_no varchar2(10),
  t_no varchar2(10)
); 

5、插入测试数据到学生老师对照表
begin
  for i in 5 .. 15 loop
    insert into t_B_comp_student_teacher
      (s_no, t_no)
    values
      ('N' || lpad(i, 9, 0), 'T' || lpad(i, 9, 0));
  end loop;
end ;

6、查询学生与老师对照表 
select * from t_B_comp_student_teacher ; 
  S_NO T_NO
1 N000000005 T000000005
2 N000000006 T000000006
3 N000000007 T000000007
4 N000000008 T000000008
5 N000000009 T000000009
6 N000000010 T000000010
7 N000000011 T000000011
8 N000000012 T000000012
9 N000000013 T000000013
10 N000000014 T000000014
11 N000000015 T000000015


7、创建学生老师整合表
create table t_B_info_student_teacher(
  s_no varchar2(10),
  s_name varchar2(100),
  s_sex varchar2(10),
  create_by varchar2(10),
  create_date date,
  t_no varchar2(10)
);

8、使用merge into插入数据到学生老师整合表
MERGE INTO t_B_info_student_teacher t
USING (select t.s_no, t.s_name, t.s_sex, t.create_by, t.create_date, t1.t_no
         from t_B_info_student t
         left join t_B_comp_student_teacher t1
           on t.s_no = t1.s_no) tw
ON (t.s_no = tw.s_no)
WHEN MATCHED THEN
  UPDATE SET t.t_no = tw.t_no
WHEN NOT MATCHED THEN
  INSERT
  VALUES
    (tw.s_no, tw.s_name, tw.s_sex, tw.create_by, tw.create_date, tw.t_no);

9、查询 学生老师整合表
select * from t_B_info_student_teacher ;
  S_NO S_NAME S_SEX CREATE_BY CREATE_DATE T_NO
10 N000000001 姓名1 admin 2016/11/11 10:17:53  
9 N000000002 姓名2 admin 2016/11/11 10:17:53  
8 N000000003 姓名3 admin 2016/11/11 10:17:53  
7 N000000004 姓名4 admin 2016/11/11 10:17:53  
1 N000000005 姓名5 admin 2016/11/11 10:17:53 T000000005
2 N000000006 姓名6 admin 2016/11/11 10:17:53 T000000006
3 N000000007 姓名7 admin 2016/11/11 10:17:53 T000000007
4 N000000008 姓名8 admin 2016/11/11 10:17:53 T000000008
5 N000000009 姓名9 admin 2016/11/11 10:17:53 T000000009
6 N000000010 姓名10 admin 2016/11/11 10:17:53 T000000010



10、插入数据到学生表、学生与老师对照表、重新查询 学生老师整合表
begin
  for i in 11 .. 20 loop
    insert into t_B_info_student
      (s_no, s_name, s_sex, create_by, create_date)
    values
      ('N' || lpad(i, 9, 0), '姓名' || i,decode(mod(i,2),0,'男','女') , 'admin', sysdate);
  end loop;
end;  

begin
  for i in  1 .. 3 loop
    insert into t_B_comp_student_teacher
      (s_no, t_no)
    values
      ('N' || lpad(i, 9, 0), 'T' || lpad(i, 9, 0));
  end loop;
end ; 


MERGE INTO t_B_info_student_teacher t
USING (select t.s_no, t.s_name, t.s_sex, t.create_by, t.create_date, t1.t_no
         from t_B_info_student t
         left join t_B_comp_student_teacher t1
           on t.s_no = t1.s_no) tw
ON (t.s_no = tw.s_no)
WHEN MATCHED THEN
  UPDATE SET t.t_no = tw.t_no
WHEN NOT MATCHED THEN
  INSERT
  VALUES
    (tw.s_no, tw.s_name, tw.s_sex, tw.create_by, tw.create_date, tw.t_no);
 
  S_NO S_NAME S_SEX CREATE_BY CREATE_DATE T_NO
10 N000000001 姓名1 admin 2016/11/11 10:17:53 T000000001
9 N000000002 姓名2 admin 2016/11/11 10:17:53 T000000002
8 N000000003 姓名3 admin 2016/11/11 10:17:53 T000000003
7 N000000004 姓名4 admin 2016/11/11 10:17:53  
1 N000000005 姓名5 admin 2016/11/11 10:17:53 T000000005
2 N000000006 姓名6 admin 2016/11/11 10:17:53 T000000006
3 N000000007 姓名7 admin 2016/11/11 10:17:53 T000000007
4 N000000008 姓名8 admin 2016/11/11 10:17:53 T000000008
5 N000000009 姓名9 admin 2016/11/11 10:17:53 T000000009
6 N000000010 姓名10 admin 2016/11/11 10:17:53 T000000010
11 N000000011 姓名11 admin 2016/11/11 11:06:19 T000000011
12 N000000012 姓名12 admin 2016/11/11 11:06:19 T000000012
13 N000000013 姓名13 admin 2016/11/11 11:06:19 T000000013
14 N000000014 姓名14 admin 2016/11/11 11:06:19 T000000014
15 N000000015 姓名15 admin 2016/11/11 11:06:19 T000000015
18 N000000016 姓名16 admin 2016/11/11 11:06:19  
19 N000000017 姓名17 admin 2016/11/11 11:06:19  
16 N000000018 姓名18 admin 2016/11/11 11:06:19  
17 N000000019 姓名19 admin 2016/11/11 11:06:19  
20 N000000020 姓名20 admin 2016/11/11 11:06:19