Oracle SQL、PLSQL 绝技之多表插入[转帖]_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
2
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 5286 | 回复: 1   主题: Oracle SQL、PLSQL 绝技之多表插入[转帖]        下一篇 
yun
注册用户
等级:少校
经验:1082
发帖:83
精华:4
注册:2012-12-17
状态:离线
发送短消息息给yun 加好友    发送短消息息给yun 发消息
发表于: IP:您无权察看 2012-12-17 16:47:48 | [全部帖] [楼主帖] 楼主

本文转自http://stevex.blog.51cto.com/4300375/1042856

假如一个在线电子商务系统,我们现在需要根据订单表体现的消费金额将客户简单分为大中小三类并分别插入到三张表中.

订单表 order (order_id number, cust_id number, amount number);
小客户表 small_cust (cust_id number, tot_amt number);
中客户表 med_cust (cust_id number, tot_amt number);
大客户表 big_cust (cust_id number, tot_amt number);

如果总消费金额小于10000, 则归入小客户;
如果总消费金额大于10000并小于50000,则归入中客户;
如果总消费金额大于50000,则归入大客户;

要实现这个需求,如果我们不知道INSERT ALL/FIRST 的用法,可能会用一段PL/SQL遍历查询订单表返回的游标,然后逐条记录判断客户消费总额来决定插入哪个表,需要分别写三个INSERT语句,这样也可以达到目的,但远没有使用INSERT FIRST简洁和高效。

下面是用INSERT FIRST实现的例子,是不是一目了然?

  1.     insert first  
  2.     when tot_amount < 10000 then  
  3.     into small_cust_test  
  4.     when tot_amount >=10000 and tot_amount <50000 then  
  5.     into med_cust_test  
  6.     else  
  7.     into big_cust_test  
  8.     select cust_id,sum(amount) as tot_amount   
  9.     from order_test  
  10.     group by cust_id;  


  1. FIRST:表示第一WHEN条件符合后就跳到下条记录,不再判断其它WHEN条件。 
  2. ALL  :表示不管前面的WHEN条件是否已经满足,后续的条件都会被判断,可能会一次出现多表同时插入。 



示例完整代码: 

  1. SQL> create table order_test (order_id number, cust_id number, amount number); 
  2. Table created 
  3. SQL> create table small_cust_test (cust_id number, tot_amt number); 
  4. Table created 
  5. SQL> create table med_cust_test (cust_id number, tot_amt number); 
  6. Table created 
  7. SQL> create table big_cust_test (cust_id number, tot_amt number); 
  8. Table created 
  9. SQL> select * from order_test order by order_id; 
  10.   ORDER_ID    CUST_ID     AMOUNT 
  11. ---------- ---------- ---------- 
  12.          1       1001       2060 
  13.          2       1002      20060 
  14.          3       1003      30060 
  15.          4       1004      50060 
  16.          5       1004      10060 
  17.          6       1005     100060 
  18.          7       1001       2000 
  19.          8       1001       2050 
  20. 8 rows selected 
  21. SQL> select cust_id, sum(amount) as tot_amt from order_test group by cust_id; 
  22.    CUST_ID    TOT_AMT 
  23. ---------- ---------- 
  24.       1003      30060 
  25.       1001       6110 
  26.       1002      20060 
  27.       1004      60120 
  28.       1005     100060 
  29. SQL> select * from small_cust_test; 
  30.    CUST_ID    TOT_AMT 
  31. ---------- ---------- 
  32. SQL> select * from med_cust_test; 
  33.    CUST_ID    TOT_AMT 
  34. ---------- ---------- 
  35. SQL> select * from big_cust_test; 
  36.    CUST_ID    TOT_AMT 
  37. ---------- ---------- 
  38. SQL> insert first 
  39.   2  when tot_amount < 10000 then 
  40.   3  into small_cust_test 
  41.   4  when tot_amount >=10000 and tot_amount <50000 then 
  42.   5  into med_cust_test 
  43.   6  else 
  44.   7  into big_cust_test 
  45.   8  select cust_id,sum(amount) as tot_amount  
  46.   9  from order_test 
  47.  10  group by cust_id; 
  48. 5 rows inserted 
  49. SQL> select * from small_cust_test; 
  50.    CUST_ID    TOT_AMT 
  51. ---------- ---------- 
  52.       1001       6110 
  53. SQL> select * from med_cust_test; 
  54.    CUST_ID    TOT_AMT 
  55. ---------- ---------- 
  56.       1003      30060 
  57.       1002      20060 
  58. SQL> select * from big_cust_test; 
  59.    CUST_ID    TOT_AMT 
  60. ---------- ---------- 
  61.       1004      60120 
  62.       1005     100060 
  63. SQL>  




赞(0)    操作        顶端 
周逸涵
注册用户
等级:少校
经验:871
发帖:83
精华:0
注册:2013-7-8
状态:离线
发送短消息息给周逸涵 加好友    发送短消息息给周逸涵 发消息
发表于: IP:您无权察看 2013-7-11 10:55:47 | [全部帖] [楼主帖] 2  楼

不错不错 北京联动北方科技有限公司



赞(0)    操作        顶端 
总帖数
2
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论