Oracle-Multitable Insert Command 操作

来源:互联网 发布:java web后端国际化 编辑:程序博客网 时间:2024/04/30 00:40

       Oracle支持在一条insert语句中将加入多条into语句,以完成在一条insert 语句中将不同的值插入至不同的表中,这样的做法比常规的分成多条的insert语句执行效率效率高,书写也简洁!下面我们主要讲解一下Multitable Insert Commend的使用方法:

      Multitable Insert Commend 主要有四种类型:

  • 无condational 的insert all
  • 有condational 的insert all
  • 有condational 的insert first
  • 最后是pivoting insert操作

     下面是几种multitable insert的示例:

               示例表:

                        create table test_multitable_source(
                                     user_name varchar2(10),
                                     sales  number,
                                     parcheses number
                        );

                        表数据示例:

                             a1    2    4
                             a3    4    4

                             a4    5    5

                            a7    2    6

                    create table test_multitable_sale(
                             user_name varchar2(10),
                             sales number
                    );

                    create table test_multitable_parchese(
                             user_name varchar2(10),
                             sales number
                     );

                   
                   create table test_multitable_other(
                                user_name varchar2(10),
                                 sales number,
                                 parchese number
                   );


              (1)无条件的multitable insert:

                     insert all
                             into test_multitable_sale(user_name,sales) values (user_name,sales)
                             into test_multitable_parchese(user_name,sales) values (user_name,parcheses)
                     select tms.user_name,tms.sales,tms.parcheses from test_multitable_source tms;
             (2)有条件的insert all:

   insert all
    when sales > 2  then
     into test_multitable_sale(user_name,sales) values (user_name,sales)
   when parcheses > 4 then
    into test_multitable_parchese(user_name,sales) values (user_name,parcheses)  

   else
    into test_multitable_other(user_name,sales,parchese) values (user_name,sales,parcheses)
  select tms.user_name,tms.sales,tms.parcheses from test_multitable_source tms;

  其中sales > 2 共有2条:

                a3    4    4

                a4    5    5

         parchese > 4 共有2条记录:

                 a4    5    5

                 a7    2    6

        而else 表示的含义为sales <= 2 and parchese <= 4,符合条件的记录只有一条:

                 a1    2    4
          (3) 有条件的insert first

                   insert first与insert all的区别为:找到第一个符合条件的when子句(clause),就返回,不再执行下面的when条件,下面的insert first语句执行成功后:
               
   insert first
    when sales > 2  then
     into test_multitable_sale(user_name,sales) values (user_name,sales)
   when parcheses > 4 then
    into test_multitable_parchese(user_name,sales) values (user_name,parcheses)  

   else
    into test_multitable_other(user_name,sales,parchese) values (user_name,sales,parcheses)
  select tms.user_name,tms.sales,tms.parcheses from test_multitable_source tms;

 返回结果为:

     when sales > 2 then返回成功2条记录,和insert all返回结果一样:

                    a3    4    4

                   a4    5    5

    when parcheses > 4 then,则含义变为:  when sales <= 2 and parcheses > 4 ,返回的结果有:

                   a7    2    6

   而 else 的含义和insert all一样,返回1条记录:

                  a1    2    4

     (4)pivoting insert

         该语句其它和无条件的insert all一样,唯一的变化是可以提供一种思路,列变行:
                表结构:

                      create table test_multitable_pivoting(
                                  user_name varchar2(10),
                                  month_id number,
                                  sale_1mon number,
                                  sale_2mon number,
                                  sale_3mon number
                      );

                  表数据:

                        王五    2            0    50    0
                        张三    3            0    0    70
                        张三    1            20    0    0

                  create table test_multitable_sale(
                        user_name varchar2(10),
                        month_id number,
                         sales number
                  );

                 对应的insert语句:

                         insert all
                                into test_multitable_sale(user_name,month_id,sales) values (user_name,month_id,sale_1mon)
                                into test_multitable_sale(user_name,month_id,sales) values (user_name,month_id,sale_2mon)
                                into test_multitable_sale(user_name,month_id,sales) values (user_name,month_id,sale_3mon)
                           select tms.* from test_multitable_pivoting tms;                  

                 最后test_multitable_sale中生成的结果为:
                            王五    50    2
                            王五    0    2
                            王五    0    2
                            张三    20    1
                            张三    0    1
                            张三    0    1
                            张三    70    3
                            张三    0    3
                            张三    0    3
                 


                     

    






0 0
原创粉丝点击