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

Oracle中merge用法

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

二、下面先演示一下merge的基本功能

 

1) matched 和not matched clauses 同时使用

   merge into acct a

     using subs b on (a.msid=b.msid)

   when MATCHED then

        update set a.areacode=b.areacode

   when NOT MATCHED then

        insert(msid,bill_month,areacode)

        values(b.msid,'200702',b.areacode);

2) 只有not matched clause,也就是只插入不更新

   merge into acct a

     using subs b on (a.msid=b.msid)  

   when NOT MATCHED then

        insert(msid,bill_month,areacode)

        values(b.msid,'200702',b.areacode);

 

3) 只有matched clause, 也就是只更新不插入

   merge into acct a

     using subs b on (a.msid=b.msid)

   when MATCHED then

        update set a.areacode=b.areacode

 

Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0

Connected as study

 

SQL> select * from subs;

 

      MSID MS_TYPE AREACODE

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

 905310001 0            531

 905320001 1            532

 905330001 2            533

 

SQL> select * from acct;

 

      MSID BILL_MONTH AREACODE        FEE

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

 

SQL>

SQL> merge into acct a

  2       using subs b on (a.msid=b.msid)

  3     when MATCHED then

  4          update set a.areacode=b.areacode

  5     when NOT MATCHED then

  6          insert(msid,bill_month,areacode)

  7          values(b.msid,'200702',b.areacode);

 

Done

 

SQL> select * from acct;

 

      MSID BILL_MONTH AREACODE        FEE

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

 905320001     200702      532       0.00

 905330001     200702      533       0.00

 905310001     200702      531       0.00

 

SQL> insert into subs values(905340001,3,534);

 

1 row inserted

 

SQL> select * from subs;

 

      MSID MS_TYPE AREACODE

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

 905340001 3            534

 905310001 0            531

 905320001 1            532

 905330001 2            533

 

SQL>

SQL> merge into acct a

  2       using subs b on (a.msid=b.msid)

  3     when NOT MATCHED then

  4          insert(msid,bill_month,areacode)

  5          values(b.msid,'200702',b.areacode);

 

Done

 

SQL> select * from acct;

 

      MSID BILL_MONTH AREACODE        FEE

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

 905320001     200702      532       0.00

 905330001     200702      533       0.00

 905310001     200702      531       0.00

 905340001     200702      534       0.00

 

SQL> update subs set areacode=999;

 

4 rows updated

 

SQL> select * from subs;

 

      MSID MS_TYPE AREACODE

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

 905340001 3            999

 905310001 0            999

 905320001 1            999

 905330001 2            999

 

SQL> select * from acct;

 

      MSID BILL_MONTH AREACODE        FEE

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

 905320001     200702      532       0.00

 905330001     200702      533       0.00

 905310001     200702      531       0.00

 905340001     200702      534       0.00

 

SQL>

SQL> merge into acct a

  2       using subs b on (a.msid=b.msid)

  3     when MATCHED then

  4          update set a.areacode=b.areacode;

 

Done

 

SQL> select * from acct;

 

      MSID BILL_MONTH AREACODE        FEE

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

 905320001     200702      999       0.00

 905330001     200702      999       0.00

 905310001     200702      999       0.00

 905340001     200702      999       0.00

 

SQL>

 

三、10g中增强一:条件操作

 

1) matched 和not matched clauses 同时使用

   merge into acct a

     using subs b on (a.msid=b.msid)    

   when MATCHED then

        update set a.areacode=b.areacode

        where b.ms_type=0

   when NOT MATCHED then

        insert(msid,bill_month,areacode)

        values(b.msid,'200702',b.areacode)

        where b.ms_type=0;

2) 只有not matched clause,也就是只插入不更新

   merge into acct a

     using subs b on (a.msid=b.msid)  

   when NOT MATCHED then

        insert(msid,bill_month,areacode)

        values(b.msid,'200702',b.areacode)

        where b.ms_type=0;

 

3) 只有matched clause, 也就是只更新不插入

   merge into acct a

     using subs b on (a.msid=b.msid)

   when MATCHED then

        update set a.areacode=b.areacode

        where b.ms_type=0;

 

 

Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0

Connected as study

 

SQL> select * from subs;

 

      MSID MS_TYPE AREACODE

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

 905310001 0            531

 905320001 1            532

 905330001 2            533

 

SQL> select * from acct;

 

      MSID BILL_MONTH AREACODE        FEE

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

 

SQL>

SQL> merge into acct a

  2       using subs b on (a.msid=b.msid)

  3     when MATCHED then

  4          update set a.areacode=b.areacode

  5          where b.ms_type=0

  6     when NOT MATCHED then

  7          insert(msid,bill_month,areacode)

  8          values(b.msid,'200702',b.areacode)

  9          where b.ms_type=0;

 

Done

 

SQL> select * from acct;

 

      MSID BILL_MONTH AREACODE        FEE

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

 905310001     200702      531       0.00

 

SQL> insert into subs values(905360001,0,536);

 

1 row inserted

 

SQL> select * from subs;

 

      MSID MS_TYPE AREACODE

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

 905360001 0            536

 905310001 0            531

 905320001 1            532

 905330001 2            533

 

SQL>

SQL> merge into acct a

  2       using subs b on (a.msid=b.msid)

  3     when NOT MATCHED then

  4          insert(msid,bill_month,areacode)

  5          values(b.msid,'200702',b.areacode)

  6          where b.ms_type=0;

 

Done

 

SQL> select * from acct;

 

      MSID BILL_MONTH AREACODE        FEE

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

 905310001     200702      531       0.00

 905360001     200702      536       0.00

 

SQL> update subs set areacode=888 where ms_type=0;

 

2 rows updated

 

SQL> select * from subs;

 

      MSID MS_TYPE AREACODE

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

 905360001 0            888

 905310001 0            888

 905320001 1            532

 905330001 2            533

 

SQL> select * from acct;

 

      MSID BILL_MONTH AREACODE        FEE

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

 905310001     200702      531       0.00

 905360001     200702      536       0.00

 

SQL>

SQL> merge into acct a

  2       using subs b on (a.msid=b.msid)

  3     when MATCHED then

  4          update set a.areacode=b.areacode

  5          where b.ms_type=0;

 

Done

 

SQL> select * from acct;

 

      MSID BILL_MONTH AREACODE        FEE

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

 905310001     200702      888       0.00

 905360001     200702      888       0.00

 

SQL>

四、10g中增强二:删除操作

An optional DELETE WHERE clause can be used to clean up after a

merge operation. Only those rows which match both the ON clause

and the DELETE WHERE clause are deleted.

 

   merge into acct a

     using subs b on (a.msid=b.msid)

   when MATCHED then

        update set a.areacode=b.areacode       

        delete where (b.ms_type!=0);            

 

SQL> select * from subs;

 

      MSID MS_TYPE AREACODE

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

 905310001 0            531

 905320001 1            532

 905330001 2            533

 

SQL> select * from acct;

 

      MSID MS_TYPE AREACODE

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

 905310001 0            531

 905320001 1            532

 905330001 2            533

 

SQL>

SQL>  merge into acct a

  2       using subs b on (a.msid=b.msid)

  3     when MATCHED then

  4          update set a.areacode=b.areacode

  5          delete where (b.ms_type!=0);

 

Done

 

SQL> select * from acct;

 

      MSID MS_TYPE AREACODE

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

 905310001 0            531

 

SQL>

 

 

 

 

其实merge除了不能return,功能还是挺强的,希望下面的东西对大家有点帮助。

 

1、无条件的Inserts

 

 

你能够不用连接源表和目标表就把源表的数据插入到目标表中. 这对于你想插入所有行到目标表时是非常有用的. Oracle 10g现在支持在ON条件中使用常量过滤谓词. 举个常量过滤谓词例子ON (1=0). 下面例子从源表插入行到表PRODUCTS, 不检查这些行是否在表PRODUCTS中存在:

 

 

 

SQL> MERGE INTO products p

    2 USING newproducts np

    3 ON (1=0)

    4 WHEN NOT MATCHED THEN

    5 INSERT

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

    7 WHERE np.category = 'BOOKS'

    SQL> /

    1 row merged.

    SQL> SELECT * FROM products;

    PRODUCT_ID PRODUCT_NAME CATEGORY

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

    1501 VIVITAR 35MM ELECTRNCS

    1502 OLYMPUS IS50 ELECTRNCS

    1600 PLAY GYM TOYS

    1601 LAMAZE TOYS

    1666 HARRY POTTER DVD

    1700 WAIT INTERFACE BOOKS

    6 rows selected.

          SQL>

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

       

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