Oracle-Multitable Insert Command 操作

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Contents iii 3 Enhancements to the GROUP BY Clause Objectives 3-2 Review of Group Functions 3-3 Review of the GROUP BY Clause 3-4 Review of the HAVING Clause 3-5 GROUP BY with ROLLUP and CUBE Operators 3-6 ROLLUP Operator 3-7 ROLLUP Operator Example 3-8 CUBE Operator 3-9 CUBE Operator: Example 3-10 GROUPING Function 3-11 GROUPING Function: Example 3-12 GROUPING SETS 3-13 GROUPING SETS: Example 3-15 Composite Columns 3-17 Composite Columns: Example 3-19 Concatenated Groupings 3-21 Concatenated Groupings Example 3-22 Summary 3-23 Practice 3 Overview 3-24 4 Advanced Subqueries Objectives 4-2 What Is a Subquery? 4-3 Subqueries 4-4 Using a Subquery 4-5 Multiple-Column Subqueries 4-6 Column Comparisons 4-7 Pairwise Comparison Subquery 4-8 Nonpairwise Comparison Subquery 4-9 Using a Subquery in the FROM Clause 4-10 Scalar Subquery Expressions 4-11 Scalar Subqueries: Examples 4-12 Correlated Subqueries 4-14 Using Correlated Subqueries 4-16 Using the EXISTS Operator 4-18 Using the NOT EXISTS Operator 4-20 Correlated UPDATE 4-21 The WITH Clause 4-26 WITH Clause: Example 4-27 Summary 4-29 Practice 4 Overview 4-31 iv 5 Hierarchical Retrieval Objectives 5-2 Sample Data from the EMPLOYEES Table 5-3 Natural Tree Structure 5-4 Hierarchical Queries 5-5 Walking the Tree 5-6 Walking the Tree: From the Bottom Up 5-8 Walking the Tree: From the Top Down 5-9 Ranking Rows with the LEVEL Pseudocolumn 5-10 Formatting Hierarchical Reports Using LEVEL and LPAD 5-11 Pruning Branches 5-13 Summary 5-14 Practice 5 Overview 5-15 6 Oracle9i Extensions to DML and DDL Statements Objectives 6-2 Review of the INSERT Statement 6-3 Review of the UPDATE Statement 6-4 Overview of Multitable INSERT Statements 6-5 Types of Multitable INSERT Statements 6-7 Multitable INSERT Statements 6-8 Unconditional INSERT ALL 6-10 Conditional INSERT ALL 6-11 Conditional FIRST INSERT 6-13 Pivoting INSERT 6-15 External Tables 6-18 Creating an External Table 6-19 Example of Creating an External Table 6-20 Querying External Tables 6-23 CREATE INDEX with CREATE TABLE Statement 6-24 Summary 6-25 Practice 6 Overview 6-26 A Practice Solutions B Table Descriptions and Data C Writing Advanced Scripts D Oracle Architectural Components Index Additional Practices Additional Practice Solutions Additional Practices: Table Descriptions and Data

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值