您好,登錄后才能下訂單哦!
merge into可以使oracle數據表中已有數據進行合并。即有則update,否則更新
下邊的示例是在jdbc數據源連接后測試通過的
1、單條數據
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、多條數據(批處理)
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);
如果出現多張表的話只需要針對using (select ? MOBILE from dual) tmp on修改即可
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。