您好,登錄后才能下訂單哦!
merge into可以使oracle數(shù)據(jù)表中已有數(shù)據(jù)進(jìn)行合并。即有則update,否則更新
下邊的示例是在jdbc數(shù)據(jù)源連接后測(cè)試通過(guò)的
1、單條數(shù)據(jù)
Long companyAuthId = SeqkeyUtil.getSeqKey(vids, "COMPANY_AUTH_ID"); CompanyAuth comtmp = companyList.get(0); String sql = "merge into COMPANY_AUTH auth using (select ? MOBILE from dual) tmp on (auth.mobile=tmp.mobile)"+ " WHEN MATCHED THEN"+ " UPDATE SET auth.reserve=?"+ " WHEN NOT MATCHED THEN"+ " INSERT (id,MOBILE) VALUES (?, ?)"; vids.update(sql, new Object[]{comtmp.getMobile(),comtmp.getReserve(),companyAuthId+"",comtmp.getMobile()});
2、多條數(shù)據(jù)(批處理)
Long companyAuthId = SeqkeyUtil.getSeqKey(vids, "COMPANY_AUTH_ID");//seqs 表主鍵使用 List<Object[]> batch = new ArrayList<Object[]>(); for (CompanyAuth company : companyList) { Object[] values = new Object[] { company.getMobile(), company.getReserve(), companyAuthId+"", company.getMobile() }; batch.add(values); } String sql = "merge into COMPANY_AUTH auth using (select ? MOBILE from dual) tmp on (auth.mobile=tmp.mobile)"+ " WHEN MATCHED THEN"+ " UPDATE SET auth.reserve=?"+ " WHEN NOT MATCHED THEN"+ " INSERT (id,MOBILE) VALUES (?, ?)"; vids.batchUpdate(sql, batch);
如果出現(xiàn)多張表的話只需要針對(duì)using (select ? MOBILE from dual) tmp on修改即可
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。