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 閱讀(210) 評論(0)  編輯  收藏 所屬分類: 05.SQL
          主站蜘蛛池模板: 当涂县| 平泉县| 屏南县| 华坪县| 修文县| 灵宝市| 邵武市| 连南| 德清县| 乃东县| 珲春市| 屯门区| 漾濞| 湖口县| 修武县| 黎平县| 布拖县| 什邡市| 钟山县| 新宁县| 永修县| 绵阳市| 荥阳市| 永丰县| 清河县| 迁西县| 玛多县| 南岸区| 海林市| 岗巴县| 儋州市| 靖宇县| 曲水县| 沭阳县| 安康市| 当涂县| 习水县| 洛浦县| 芮城县| 永泰县| 铜梁县|