新聞中心
由于merge into平時很少用,但這次用到它來給記錄做插入更新,于是簡單記下最基本的用法。這里的例子就是給一個表中符合條件的數(shù)據(jù)做個值計數(shù)的更新,如果找到符合ID條件的記錄,那么就將其值字段加1,否則就插入這條新的記錄,并初始化值。

創(chuàng)新互聯(lián)致力于成都做網(wǎng)站、網(wǎng)站建設、外貿(mào)營銷網(wǎng)站建設,成都網(wǎng)站設計,集團網(wǎng)站建設等服務標準化,推過標準化降低中小企業(yè)的建站的成本,并持續(xù)提升建站的定制化服務水平進行質(zhì)量交付,讓企業(yè)網(wǎng)站從市場競爭中脫穎而出。 選擇創(chuàng)新互聯(lián),就選擇了安全、穩(wěn)定、美觀的網(wǎng)站建設服務!
創(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
分享名稱:mergeinto基本用法
轉(zhuǎn)載來源:http://www.dlmjj.cn/article/gcieci.html


咨詢
建站咨詢
