Oracle的Merge into用法總結

2021-08-27 20:26:53 字數 3513 閱讀 7179

簡單的說就是,判斷表中有沒有符合on()條件中的資料,有了就更新資料,沒有就插入資料。  

有乙個表t,有兩個欄位a、b,我們想在表t中做insert/update,如果條件滿足,則更新t中b的值,否則在t中插入一條記錄。在microsoft的sql語法中,很簡單的一句判斷就可以了,sql server中的語法如下:

if exists(select 1 from t where t.a ='1001' )

update t set t.b=2 where t.a = '1001'

else

insert into t(a,b) values('1001',2);

但是很明顯這個語法對於sql只能更改一條語句,並且oracle不能使用.所以就有了merge into(oracle 9i引入的功能)語法 :

merge into 目標表 a

using 源表 b

on(a.條件欄位1=b.條件欄位1 and a.條件欄位2=b.條件欄位2 ……)

when matched then update set a.更新字段=b.欄位

when not macthed then insert into a(欄位1,欄位2……)values(值1,值2……)

"在乙個同時存在insert和update語法的merge語句中,總共insert/update的記錄數,就是using語句中"源表"的記錄數"源表b可能是一張表結構不同於a的表,有可能是一張構建相同表結構的臨時表,也有可能是我們自己組起來的資料.

對於前兩種很好理解。現在主要說一下元件資料。

對於oracle有dual這個系統表很好構建,如下:

mergeinto t t1

using (select '1001' as a,2 as b from dual) t2

on ( t1.a = t2.a)

when matched then

update set t1.b = t2.b

when not matched then

insert (a,b) values(t2.a,t2.b);

merge 的其他功能

merge語句還有乙個強大的功能是通過output子句,可以將剛剛做過變動的資料進行輸出。我們在上面的merge語句後加入output子句:

select * from targettable 

select * from sourcetable

merge into targettable as t

using sourcetable as s

on t.id = s.id

when matched ---當上面的on後面的t.id=s.id時,目標表中的id為1,2的資料被更新

then not matched --目標表中沒有的id,在原來表有,則插入相關資料

then insert values(s.id,s.[desc])

when not matched by source --目標表中存在,原表中不存在則刪除

then delete

output $action as [action],inserted.id as 插入的id,

inserted.[desc] as 插入的desc,

deleted.id as 刪除的id,

deleted.[desc] as 刪除的desc;

此時merge操作完成後,將所變動的語句進行輸出:

當然了,上面的merge關鍵字後面使用了多個when…then語句,而這個語句是可選的.也可以僅僅新增或是僅僅刪除:

merge into targettable as t

using sourcetable as s

on t.id = s.id

then not matched --目標表中沒有的id,在原來表有,則插入相關資料

then insert values(s.id,s.[desc])

output $action as [action],inserted.id as 插入的id,

inserted.[desc] as 插入的desc,

deleted.id as 刪除的id,

deleted.[desc] as 刪除的desc;

我們還可以使用top關鍵字限制目標表被操作的行,如圖8所示。在圖2的語句基礎上加上了top關鍵字,我們看到只有兩行被更新:

merge top(2) targettable as t

using sourcetable as s

on t.id = s.id

when matched ---當上面的on後面的t.id=s.id時,目標表中的id為1,2的資料被更新

then not matched --目標表中沒有的id,在原來表有,則插入相關資料

then insert values(s.id,s.[desc])

when not matched by source --目標表中存在,原表中不存在則刪除

then delete

output $action as [action],inserted.id as 插入的id,

inserted.[desc] as 插入的desc,

deleted.id as 刪除的id,

deleted.[desc] as 刪除的desc;

但僅僅是matched這種限制條件往往不能滿足實際需求,我們可以在圖7那個語句的基礎上加上and附加上額外的限制條件:

merge into targettable as t

using sourcetable as s

on t.id = s.id

then not matched and s.id = 3 --加入了id=3的限制條件

then insert values(s.id,s.[desc])

output $action as [action],inserted.id as 插入的id,

inserted.[desc] as 插入的desc,

deleted.id as 刪除的id,

deleted.[desc] as 刪除的desc;

如何將批處理做成類流處理 merge into

例如 隨著人工智慧的不斷發展,機器學習這門技術也越來越重要,很多人都開啟了學習機器學習,本文就介紹了機器學習的基礎內容。需要在前端頁面上設計成實時的資料,但是沒有flink,所以只能把批處理做成類流處理 更新存在的 update employe as em set salary select sal...

oracle學習總結 oracle的介紹

1 資料庫的三層結構 client 專用於訪問資料庫 dbms database management system db例項 多個 db例項有很多資料物件 例 表,包,檢視,序列,函式,觸發器,索引 2 在專案中如何選擇資料庫 1 標的 2 功能 3 併發性問題 4 安全 穩定 5 作業系統 un...

oracle 2 oracle的使用者

1 dbca oracle的dbca主要用來管理資料庫,包括建立資料庫 刪除資料庫等。注意 建立資料庫的時候,密碼不能全是數字,也不能以數字開頭 2 ofa oracle flexible architecture oracle優化靈活結構 作用 多oracle版本的管理 ora90 資料庫管理工具...