merge into的用法及10g新特性總結

2022-07-03 19:57:13 字數 3677 閱讀 2355

merge into

的作用:

將源資料(**於實際的表,檢視,子查詢)更新或插入到指定的表中(必須實際存在),依賴於on條件,好處是避免了多個insert 和update操作。

merge是乙個目標性明確的操作符,不允許在乙個merge 語句中對相同的行insert或update操作。

這個語法僅需要一次全表掃瞄就完成了全部工作,執行效率要高於insert+

update

. 尤其是在大資料量面前,效率越明顯.

具體用法如下:

create

table t as

select rownum id, a.*

from

user_objects a;

create

table t1 as

select rownum id, table_name, cast('

table'as

varchar2(100)) object_type from

user_tables;

select

*from

t;select

*from

t1;--

meger into的用法

--1.能獲得穩定的行時,使用下列語句

merge into t1 using t on (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.object_name

, t.object_type);

--2.不能獲得穩定行時,使用下列語句

merge into

t1using (

select

object_name, object_type, max(id) id from t group

byobject_name

, object_type) t

on (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.object_name

, t.object_type);

--值得注意的是: merge語句中的update不能修改用於連線的列,否則會報錯.

--建立測試表和插入模擬資料

create

table subs(msid number(9), ms_type char(1), areacode number(3

));create

table acct(msid number(9), bill_month number(6), areacode number(3), fee number(8,2) default

0.00

);insert

into subs values(905310001,0,001

);insert

into subs values(905320001,1,002

);insert

into subs values(905330001,2,003

);commit;--

a.操作的表(目標表):使用原始資料**的表,並且制定條件,條件必須有括號

merge into acct a using subs b on (a.msid=

b.msid)

--當匹配的時候,執行update操作,和直接update的語法不一樣,不需要制定表名

when matched then

update

set a.areacode=

b.areacode

--當不匹配的時候,執行insert操作,也不需要制定表名,若指定字段插入,則在insert後用括號標明,不指定是全部插入

when

not matched then

insert(msid,bill_month,areacode) values(b.msid,'

201005

',b.areacode);

select

*from

acct;

select

*from

subs;

truncate

table

acct;

--merge into 10g新特性

--1.單個操作

--(1).單個not matched的時候,只做插入

merge into acct a using subs b on(a.msid=

b.msid)

when

not matched then

insert(a.msid,a.bill_month,a.areacode) values(b.msid,'

201005

',b.areacode);

--(2).單個matched的時候,只做更新操作

merge into acct a using subs b on (a.msid =

b.msid)

when matched then

update

set a.areacode =

b.areacode;

select

*from subs where ms_type=0;

--2.merge操作之後,只有匹配的update操作才可以,用delete where子句刪除目標表中(操作的表)滿足條件的行。

merge into acct a using subs b on (a.msid =

b.msid)

when matched then

update

set a.areacode = b.areacode delete

where (b.ms_type !=0)

when

not matched then

insert(msid, bill_month, areacode) values (b.msid, '

201005

', b.areacode) where b.ms_type =0;

--3. 帶上where,滿足條件的插入和更新

merge into acct a using subs b on (a.msid=

b.msid)

when matched then

update

set a.areacode=b.areacode where b.ms_type=

0when

not matched then

insert(msid,bill_month,areacode) values(b.msid,'

201005

',b.areacode) where b.ms_type=

0;

merge into 用法深思

線上出現了乙個問題 mq同步某一功能的資料的時候,同一主鍵的資料會發多次。業務邏輯是,某一條資料過來後,我會先去資料庫查是否存在了此uuid的資料,如果有的話更新,如果沒有的話插入。由於同一主鍵的資料會發不止一次的mq,並且間隔時間非常小,所以就導致了,兩條資料都進入了插入的邏輯裡面。丟擲主鍵衝突的...

oracle裡的merge into用法

1 根據表newproducts的product id欄位是否匹配來updates表products的資訊 sql merge into products p 2 using newproducts np 3 on p.product id np.product id 4 when matched ...

oracle 中merge into 的用法

用途 merge 命令可以用來用乙個表中的資料來修改或者插入到另乙個表。插入或者修改的操作取決於on子句的條件。該語句可以在同一語句中執行兩步操作,可以減少執行多條insert 和update語句。merge是乙個確定性的語句,即不會在同一條merge語句中去對同一條記錄多次做修改操作。語法 1.i...