Decode360's Blog

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

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 ::  :: 管理 ::
            302 隨筆 :: 26 文章 :: 82 評論 :: 0 Trackbacks
          ??? 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

          ?

          ?

          ?





          -The End-

          posted on 2008-10-23 16:51 decode360-3 閱讀(2633) 評論(0)  編輯  收藏 所屬分類: SQL Dev
          主站蜘蛛池模板: 淮北市| 楚雄市| 正宁县| 如皋市| 且末县| 龙口市| 攀枝花市| 东阳市| 合阳县| 高青县| 卓尼县| 城固县| 定兴县| 二手房| 莱芜市| 古交市| 马鞍山市| 扬中市| 金寨县| 平陆县| 元朗区| 铁岭县| 横山县| 子洲县| 屏南县| 黔西| 旬邑县| 绥芬河市| 喀什市| 夏邑县| 乌苏市| 瑞金市| 封开县| 融水| 农安县| 辽源市| 民县| 耒阳市| 沅江市| 宜黄县| 桐乡市|