oracle 9i/10g merge 用法
oracle 9i/10g merge 用法
oracle9i的merge
MERGE語句是Oracle9i新增的語法,用來合併UPDATE和INSERT語句。透過MERGE語句,根據一張表或子查詢的連線條件對另外一張表進行查詢,連線條件匹配上的進行UPDATE,無法匹配的執行INSERT。這個語法僅需要一次全表掃描就完成了全部工作,執行效率要高於INSERT+UPDATE。
[@more@]Oracle 9i/10g merge 用法
oracle9i的merge
MERGE語句是Oracle9i新增的語法,用來合併UPDATE和INSERT語句。透過MERGE語句,根據一張表或子查詢的連線條件對另外一張表進行查詢,連線條件匹配上的進行UPDATE,無法匹配的執行INSERT。這個語法僅需要一次全表掃描就完成了全部工作,執行效率要高於INSERT+UPDATE。
語法如下:
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中Oracle對merge語句做了增強,增加了條件選項和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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 轉:oracle 9i/10g merge 用法Oracle
- oracle merge into用法Oracle
- oracle中merge into用法解析Oracle
- oracle merge into 用法詳解Oracle
- ORACLE merge用法詳解Oracle
- oracle merge into用法(R1)Oracle
- oracle-merge用法詳解Oracle
- oracle-merge用法詳解 (轉)Oracle
- Oracle Flashback (9i & 10g) [zt]Oracle
- oracle 10g merge命令的增強Oracle 10g
- oracle 9i與10g,11g不同的子查詢括號用法Oracle
- upgrade oracle version 9i to 10gOracle
- Oracle 9i/10g的官方教材Oracle
- SQLServer MERGE 用法SQLServer
- Oracle 9i, 10g jdbc driver 檔案OracleJDBC
- 【原】關於Oracle Merge操作的簡單用法Oracle
- oracle 10g的dmp如何匯入9iOracle 10g
- Oracle 補丁全集 (Oracle 9i 10g 11g Path)Oracle
- Oracle 10g RAC中Srvctl基本用法Oracle 10g
- Oracle Advanced Replication 1 例 9i to 10g MVROracleVR
- SQL中Merge的用法SQL
- sql server merge 的用法SQLServer
- 區別oracle 9i 與 oracle 10g 備份表空間Oracle 10g
- oracle 10g裡,Merge語句的重大改進 ztOracle 10g
- oracle中merge的用法,以及各版本的區別 CreateOracle
- oracle 10g awr效能收集工具的用法Oracle 10g
- Oracle 9i、10g 常用軟體補丁下載地址Oracle
- oracle 9i 和oracle 10g 和oracle 11g有什麼區別Oracle 10g
- Oracle 9i和10G軟體及補丁下載地址Oracle
- [筆記]Semaphores Tunning on RedHat Linux for Oracle 9i or 10g筆記RedhatLinuxOracle
- Oracle 資料庫歸檔配置-9i,10g,11gOracle資料庫
- Oracle Flashback 閃回查詢功能操作範例(9i and 10g)Oracle
- oracle 9i 10G 11G 的RAC 穩定性比較Oracle
- Oracle 9i、10g、11g補丁集下載大全Oracle
- Oracle 9i和10g安裝介質and補丁下載大全Oracle
- oracle_mergeOracle
- Oracle 隱藏引數(9i,10g,11g,12c)Oracle
- [轉載]Oracle 9i和10G軟體及補丁下載地址Oracle