手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Oracle中merge用法

[日期:2012-06-12] 来源:Linux社区  作者:boss [字体:大 中 小]

2、新增加的DELETE子句

 

 

Oracle 10g中的MERGE提供了在执行数据操作时清除行的选项. 你能够在WHEN MATCHED THEN UPDATE子句中包含DELETE子句. DELETE子句必须有一个WHERE条件来删除匹配某些条件的行.匹配DELETE WHERE条件但不匹配ON条件的行不会被从表中删除.

 

 

下面例子验证DELETE子句. 我们从表NEWPRODUCTS中合并行到表PRODUCTS中, 但删除category为ELECTRNCS的行.

 

 

SQL> MERGE INTO products p

    2 USING newproducts np

    3 ON (p.product_id = np.product_id)

    4 WHEN MATCHED THEN

    5 UPDATE

    6 SET p.product_name = np.product_name,

    7 p.category = np.category

    8 DELETE WHERE (p.category = 'ELECTRNCS')

    9 WHEN NOT MATCHED THEN

    10 INSERT

    11 VALUES (np.product_id, np.product_name, np.category)

    SQL> /

    4 rows merged.

    SQL> SELECT * FROM products;

    PRODUCT_ID PRODUCT_NAME CATEGORY

    ---------- -------------------- ----------

    1501 VIVITAR 35MM ELECTRNCS

    1600 PLAY GYM TOYS

    1601 LAMAZE TOYS

    1666 HARRY POTTER TOYS

    1700 WAIT INTERFACE BOOKS

          SQL>

 

 

 

 

产品ID为1502的行从表PRODUCTS中被删除, 因为它同时匹配ON条件和DELETE WHERE条件. 产品ID为1501的行匹配DELETE WHERE条件但不匹配ON条件, 所以它没有被删除. 产品ID为1700 的行不匹配ON条件, 所以被插入表PRODUCTS. 产品ID为1601和1666的行匹配ON条件但不匹配DELETE WHERE条件, 所以被更新为表NEWPRODUCTS中的值.

 

3. 再来个toad中的例子吧.

Examples

Merging into a Table: Example The following example uses the bonuses table in the sample schema oe with a default bonus of 100. It then inserts into the bonuses table all employees who made sales, based on the sales_rep_id column of the oe.orders table. Finally, the human resources manager decides that employees with a salary of $8000 or less should receive a bonus. Those who have not made sales get a bonus of 1% of their salary. Those who already made sales get an increase in their bonus equal to 1% of their salary. The MERGE statement implements these changes in one step:

CREATE TABLE bonuses (employee_id NUMBER, bonus NUMBER DEFAULT 100);

 

INSERT INTO bonuses(employee_id)

   (SELECT e.employee_id FROM employees e, orders o

   WHERE e.employee_id = o.sales_rep_id

   GROUP BY e.employee_id);

 

SELECT * FROM bonuses;

 

EMPLOYEE_ID      BONUS

----------- ----------

        153        100

        154        100

        155        100

        156        100

        158        100

        159        100

        160        100

        161        100

        163        100

 

MERGE INTO bonuses D

   USING (SELECT employee_id, salary, department_id FROM employees

   WHERE department_id = 80) S

   ON (D.employee_id = S.employee_id)

   WHEN MATCHED THEN UPDATE SET D.bonus = D.bonus + S.salary*.01

     DELETE WHERE (S.salary > 8000)

   WHEN NOT MATCHED THEN INSERT (D.employee_id, D.bonus)

     VALUES (S.employee_id, S.salary*0.1)

     WHERE (S.salary <= 8000);

 

EMPLOYEE_ID      BONUS

----------- ----------

        153        180

        154        175

        155        170

        159        180

        160        175

        161        170

        179        620

        173        610

        165        680

        166        640

        164        720

        172        730

        167        620

        171        740

 

 

 

 

作用:merge into 解决用B表跟新A表数据,如果A表中没有,则把B表的数据插入A表;

 

语法:

 

MERGE INTO [your table-name] [rename your table here]

 

USING ( [write your query here] )[rename your query-sql and using just like a table]

 

ON ([conditional expression here] AND [...]...)

 

WHEN MATHED THEN [here you can execute some update sql or something else ]

 

WHEN NOT MATHED THEN [execute something else here ! ]

 

-------------------------------------实例-----------------------------------------------------------------

 

merge into tfa_alarm_act_nms a

using (select FP0,FP1,FP2,FP3,REDEFINE_SEVERITY

from tfa_alarm_status) b

on (a.fp0=b.fp0 and a.fp1=b.fp1 and a.fp2=b.fp2 and a.fp3=b.fp3)

when matched then update set a.redefine_severity=b.redefine_severity

when not matched then insert (a.fp0,a.fp1,a.fp2,a.fp3,a.org_severity,a.redefine_severity,a.event_time

,a.int_id)

values (b.fp0,b.fp1,b.fp2,b.fp3,b.REDEFINE_SEVERITY,b.redefine_severity,sysdate,7777778);

 

作用:利用表 tfa_alarm_status跟新表tfa_alarm_act_nms 的b.redefine_severity,条件是a.fp0=b.fp0 and a.fp1=b.fp1 and a.fp2=b.fp2 and a.fp3=b.fp3,如果tfa_alarm_act_nms表中没有该条件的数据就插入。

 

如果你的数据量很大,此sql效率非常高。

 

把数据从一个表复制到另一个表,插入新数据或替换掉老数据是每一个ORACLE DBA都会经常碰到的问题。在ORACLE9i以前的年代,我们要先查找是否存在老数据,如果有用UPDATE替换,否则用INSERT语句插入,其间少不了还有一些标记变量等等,繁琐的很。现在ORACLE9i专为这种情况提供了MERGE语句,使这一工作变得异常轻松。

在Oracle 10g中MERGE有如下一些改进:

1、UPDATE或INSERT子句是可选的

SQL> MERGE INTO products p

      2 USING newproducts np

      3 ON (p.product_id = np.product_id)

      4 WHEN MATCHED THEN

      5 UPDATE

      6 SET p.product_name = np.product_name,

      7 p.category = np.category;

2、UPDATE和INSERT子句可以加WHERE子句

SQL> MERGE INTO products p

      2 USING newproducts np

      3 ON (p.product_id = np.product_id)

      4 WHEN MATCHED THEN

      5 UPDATE

      6 SET p.product_name = np.product_name,

      7 p.category = np.category

      8 WHERE p.category = 'DVD'

      9 WHEN NOT MATCHED THEN

      10 INSERT

      11 VALUES (np.product_id, np.product_name, np.category)

      12 WHERE np.category != 'BOOKS'

3、在ON条件中使用常量过滤谓词来insert所有的行到目标表中,不需要连接源表和目标表

linux
相关资讯       Oracle存储过程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款