jdbc使用oracle中merge into函数

merge into能够使oracle数据表中已有数据进行合并。即有则update,不然更新java


下边的示例是在jdbc数据源链接后测试经过的sql


一、单条数据
oracle

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()});

二、多条数据(批处理)ide

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修改便可测试

相关文章
相关标签/搜索