Decode360's Blog

          業精于勤而荒于嬉 QQ:150355677 MSN:decode360@hotmail.com

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 ::  :: 管理 ::
            397 隨筆 :: 33 文章 :: 29 評論 :: 0 Trackbacks
          MERGE函數用法
          ?
          ??? PLSQL中操作數據轉化過程中經常會用到MERGE語句, MERGE函數是Oracle9i新增的函數,基本上就是等于update和insert兩個函數的相加。使用MERGE函數,通過判斷兩個表之間某字段的關聯情況,如果關聯匹配上的話就進行update操作,如果沒有匹配就執行insert。這個函數一來是減少了程序的長度,而且只對表進行一次全表掃描,效率也會有所提高。

          1、 簡單舉例:
          ?

          ??? create table t1_a as

          ??? ? select rownum id ,table_name name

          ??? ??? from user_tables;

          ?

          ??? create table t1_b as

          ??? ? select rownum id ,table_name name

          ??? ??? from user_tables

          ??? ?? where table_name like 'T%' ; -- t1_a 記錄少

          ?

          ??? merge into t1_b

          ??? using t1_a

          ??? on (t1_b.name = t1_a.name)

          ??? when matched then

          ??? ? update set t1_b.id = t1_b.id + 100000

          ??? when not matched then

          ??? ? insert values (t1_a.id, t1_a.name);

          ??? --t1_b 表中沒有的記錄插入,有的記錄把 id+100000

          ?
          ??? 注:被修改的必然是在前面的表,后面的表是附加進來進行判斷的。
          ?
          ?
          2、只寫一半:
          ?
          ??? 假設在匹配時不想進行操作,則:
          ?

          ??? merge into t1_b

          ??? using t1_a

          ??? on (t1_b.name = t1_a.name)

          ??? --when matched then

          ??? --? update set t1_b.id = t1_b.id

          ??? when not matched then

          ??? ? insert values (t1_a.id, t1_a.name);

          ?
          ??? 如果使用9i版本,此時報錯:ORA-00905: missing keyword
          ??? 如果使用10g,則順利執行。
          ?
          ?
          3、多值報錯:
          ?

          ??? truncate table t1_a;

          ??? truncate table t1_b;

          ??? insert into t1_a values ( 1 , 'a' );

          ??? insert into t1_b values ( 1 , 'c' );

          ??? insert into t1_b values ( 1 , 'b' );

          ??? commit ;

          ??? select * from t1_a;

          ??? select * from t1_b;

          ?

          ??? merge into t1_a

          ??? using t1_b

          ??? on (t1_b.id = t1_a.id)

          ??? when matched then

          ??? ? update set t1_a.name = t1_b.name

          ??? when not matched then

          ??? ? insert values (t1_b.id, t1_b.name);

          ??? ?

          ??? --ORA-30926: unable to get a stable set of rows in the source tables

          ?

          ?

          4、不能修改作為關聯的列

          ?

          ??? truncate table t1_a;

          ??? truncate table t1_b;

          ??? insert into t1_a values ( 1 , 'a' );

          ??? insert into t1_b values ( 1 , 'b' );

          ??? commit ;

          ?

          ??? merge into t1_a

          ??? using t1_b

          ??? on (t1_b.id = t1_a.id)

          ??? when matched then

          ??? ? update set t1_a.id = t1_b.id

          ??? when not matched then

          ??? ? insert values (t1_b.id, t1_b.name);

          ??? --ORA-00904: "T1_A"."ID": invalid identifier

          ?

          ?

          ?

          posted on 2008-10-23 19:51 decode360 閱讀(198) 評論(0)  編輯  收藏 所屬分類: 05.SQL
          主站蜘蛛池模板: 花莲县| 靖州| 三河市| 曲松县| 台江县| 镇雄县| 定结县| 乐清市| 翁牛特旗| 江川县| 梅河口市| 渭源县| 宜阳县| 永寿县| 周至县| 徐州市| 祁阳县| 通城县| 五华县| 延津县| 聊城市| 兰州市| 鱼台县| 嫩江县| 安图县| 宜川县| 莱阳市| 隆化县| 板桥市| 逊克县| 民勤县| 德格县| 申扎县| 奉化市| 惠东县| 辉南县| 东源县| 拉萨市| 崇左市| 景谷| 丹阳市|