2009年12月4日 星期五

Bug has Fixed when using Merge Statement with Database Link - Oracle 10g


/*
先前已經測過 Merge 的語法透過 DB2 Federation 做更新是不可行的.
Oracle 的話要上patch,聽說11g已經沒有這個bug了

My Scenario:建兩個 Database 與 兩個 Table 分別如下:
  DB1: Orion  UserName:donkey   Table: Oriontb
  DB2: Odyssey UserName:voyager  Table: Odysseytb
測試使用傳統update語法與Merge Update
*/


/* 用User:voyager登進odyssey建立測試table與資料 */

create table odysseytb
(
  col1 integer not null primary key,
  col2 varchar2(32)
);
insert into odysseytb values (1,'1 from odysseytb');
insert into odysseytb values (2,'2 from odysseytb');
commit;

 
/* 用User:donkey登進orion建立database link及測試table與資料 */

create table oriontb
(
  col1 integer not null primary key,
  col2 varchar2(32)
);
insert into oriontb values (1,'1 from oriontb');
commit;

 

1. user donkey 已經由 grant create database link to donkey 而有權限 create database link
2. 使用 odyssey 資料庫 user voyager的 權限


create database link odyssey
connect to voyager
identified by voyager
using 'odyssey'

 
/* 測試連線是否成功 */

select * from odysseytb@odyssey;

   COL1 COL2
---------- --------------------------------
     1 1 from odysseytb
     2 2 from odysseytb

 
/* 測試傳統update語法,用oriontb去update odysseytb@odyssey  */

update (select oriontb.col1,oriontb.col2,
         t.col1 link_col1,t.col2 link_col2
    from oriontb,odysseytb@odyssey t
    where oriontb.col1 = t.col1) set link_col2 = col2

已更新 1 個資料列.

select * from odysseytb@odyssey;

   COL1 COL2
---------- ---------------------------------
     1 1 from oriontb
     2 2 from odysseytb

 
/* Rollback。範例資料還需再使用 */

/* 測試傳統update語法,用odysseytb@odyssey來update oriontb */

update (select oriontb.col1,oriontb.col2,
         t.col1 link_col1,t.col2 link_col2
    from oriontb,odysseytb@odyssey t
    where oriontb.col1 = t.col1) set col2 = link_col2;

已更新 1 個資料列.

select * from oriontb;

   COL1 COL2
---------- ----------------------------------
     1 1 from odysseytb


 
/* 測試 Merge Update 語法,結果得到以下錯誤 */


merge into oriontb using odysseytb@odyssey t on (oriontb.col1 = t.col1)
when matched then
update set oriontb.col2 = t.col2;


merge into oriontb using odysseytb@odyssey t on (oriontb.col1 = t.col1)
*
ERROR 在行 1:
ORA-02064: 不支援分散式作業

 
/* 測試 Merge Insert 語法,結果得到以下錯誤 */

merge into oriontb using odysseytb@odyssey t on (oriontb.col1 = t.col1)
when not matched then insert (oriontb.col1,oriontb.col2)
values (t.col1,t.col2);

merge into oriontb using odysseytb@odyssey t on (oriontb.col1 = t.col1)
*
ERROR 在行 1:
ORA-02064: 不支援分散式作業