oracle 9i/10g merge 用法

guyuanli發表於2008-07-31

oracle 9i/10g merge 用法

oracle9imerge

MERGE語句是Oracle9i新增的語法,用來合併UPDATEINSERT語句。透過MERGE語句,根據一張表或子查詢的連線條件對另外一張表進行查詢,連線條件匹配上的進行UPDATE,無法匹配的執行INSERT。這個語法僅需要一次全表掃描就完成了全部工作,執行效率要高於INSERTUPDATE

[@more@]

Oracle 9i/10g merge 用法

oracle9imerge

MERGE語句是Oracle9i新增的語法,用來合併UPDATEINSERT語句。透過MERGE語句,根據一張表或子查詢的連線條件對另外一張表進行查詢,連線條件匹配上的進行UPDATE,無法匹配的執行INSERT。這個語法僅需要一次全表掃描就完成了全部工作,執行效率要高於INSERTUPDATE

語法如下:

MERGE INTO table_name AS table_alias
USING (table|view|sub_query) AS aliasON (join condition)

WHEN MATCHED THEN

UPDATE SET
col1 = col_val1,col2 = col2_val

WHEN NOT MATCHED THEN

INSERT (column_list) VALUES (column_values);

下面看個具體的例子: http://blog.itpub.net/post/468/14844

CREATE TABLE T AS SELECT ROWNUM ID, A.* FROM DBA_OBJECTS A;

表已建立。

CREATE TABLE T1 AS SELECT ROWNUM ID, OWNER, TABLE_NAME, CAST('TABLE' AS VARCHAR2(100)) OBJECT_TYPE FROM DBA_TABLES;

表已建立。

MERGE INTO T1 USING T ON (T.OWNER = T1.OWNER AND T.OBJECT_NAME = T1.TABLE_NAME AND T.OBJECT_TYPE = T1.OBJECT_TYPE)

WHEN MATCHED THEN

UPDATE SET T1.ID = T.ID

WHEN NOT MATCHED THEN

INSERT VALUES (T.ID, T.OWNER, T.OBJECT_NAME, T.OBJECT_TYPE);

6165 行已合併。

SELECT ID, OWNER, OBJECT_NAME, OBJECT_TYPE FROM T
MINUS

SELECT * FROM T1;

未選定行
MERGE
語法其實很簡單,下面稍微修改一下例子。

SQL> DROP TABLE T;

表已丟棄。

SQL> DROP TABLE T1;

表已丟棄。

SQL> CREATE TABLE T AS SELECT ROWNUM ID, A.* FROM DBA_OBJECTS A;

表已建立。

SQL> CREATE TABLE T1 AS SELECT ROWNUM ID, OWNER, TABLE_NAME FROM DBA_TABLES;

表已建立。

SQL> MERGE INTO T1 USING T
2 ON (T.OWNER = T1.OWNER AND T.OBJECT_NAME = T1.TABLE_NAME)
3 WHEN MATCHED THEN UPDATE SET T1.ID = T.ID
4 WHEN NOT MATCHED THEN INSERT VALUES (T.ID, T.OWNER, T.OBJECT_NAME);
MERGE INTO T1 USING T
*
ERROR
位於第 1 :
ORA-30926:
無法在源表中獲得一組穩定的行這個錯誤是使用MERGE最常見的錯誤,造成這個錯誤的原因是由於透過連線條件得到的T的記錄不唯一。

最簡單的解決方法類似:

SQL> MERGE INTO T1
2 USING (SELECT OWNER, OBJECT_NAME, MAX(ID) ID FROM T GROUP BY OWNER, OBJECT_NAME) T
3 ON (T.OWNER = T1.OWNER AND T.OBJECT_NAME = T1.TABLE_NAME)
4 WHEN MATCHED THEN UPDATE SET T1.ID = T.ID
5 WHEN NOT MATCHED THEN INSERT VALUES (T.ID, T.OWNER, T.OBJECT_NAME);
5775
行已合併。

另外,MERGE語句的UPDATE不能修改用於連線的列,否則會報錯,詳細資訊可以參考:

===============================================================

ref: http://tomszrp.itpub.net/post/11835/263865

Oracle 10g之前,merge語句支援匹配更新和不匹配插入2種簡單的用法,在10g中Oraclemerge語句做了增強,增加了條件選項和DELETE操作。下面我透過一個demo來簡單介紹一下10g中merge的增強和10g前merge的用法。

 

參考Oracle SQL Reference,大家可以看到Merge Statement的語法如下:MERGE [hint] INTO [schema .] table [t_alias] USING [schema .] { table | view | subquery } [t_alias] ON ( condition ) WHEN MATCHED THEN merge_update_clause WHEN NOT MATCHED THEN merge_insert_clause;下面我在windows xp 下10.2.0.1版本上做一個測試看看

SQL> select * from v$version;

BANNER

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

Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod

PL/SQL Release 10.2.0.1.0 - Production

CORE 10.2.0.1.0 Production

TNS for 32-bit Windows: Version 10.2.0.1.0 - Production

NLSRTL Version 10.2.0.1.0 - Production

SQL>

一、建立測試用的表

SQL> create table subs(msid number(9),

2 ms_type char(1),

3 areacode number(3)

4 );

表已建立。

SQL> create table acct(msid number(9),

2 bill_month number(6),

3 areacode number(3),

4 fee number(8,2) default 0.00);

表已建立。

SQL>

SQL> insert into subs values(905310001,0,531);

已建立 1 行。

SQL> insert into subs values(905320001,1,532);

已建立 1 行。

SQL> insert into subs values(905330001,2,533);

已建立 1 行。

SQL> commit;

提交完成。

SQL>

 

二、下面先演示一下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

9053300

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7490392/viewspace-1008231/,如需轉載,請註明出處,否則將追究法律責任。

相關文章