您好,登錄后才能下訂單哦!
由于merge into平時很少用,但這次用到它來給記錄做插入更新,于是簡單記下最基本的用法。這里的例子就是給一個表中符合條件的數(shù)據(jù)做個值計數(shù)的更新,如果找到符合ID條件的記錄,那么就將其值字段加1,否則就插入這條新的記錄,并初始化值。
創(chuàng)建測試表并插入數(shù)據(jù):
create table test1(id number, val number);
insert into test1 values(101, 1);
insert into test1 values(102, 1);
commit;
select * from test1;
ID VAL
---------- ----------
101 1
102 1
做merge into操作,新的一條數(shù)據(jù)被插入:
merge into test1 t1
using (select count(*) cnt from test1 where id = 103) t2 on (cnt <> 0)
when matched then
update set val = val + 1 where id = 103
when not matched then
insert values(103, 1);
commit;
select * from test1;
ID VAL
---------- ----------
101 1
102 1
103 1
再執(zhí)行一個merge into后,數(shù)據(jù)被更新:
ID VAL
---------- ----------
101 1
102 1
103 2
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。