若存在则修改,若不存在则插入

原创
2015/07/15 18:29
阅读数 37
oracle为我们提供了一个关键字merge
conn = DBConn.getConnection("db3");
                if (ol.get(0) instanceof Branch) {
                    StringBuffer sb = new StringBuffer();
                    sb.append("MERGE INTO BRANCH T1 ");
                    sb.append("USING (SELECT ? ID,? NAME,? A_ID FROM DUAL) T2 ");
                    sb.append("ON (T1.BRANCH_ID=T2.ID) ");
                    sb.append("WHEN MATCHED THEN UPDATE SET T1.BRANCH_NAME=T2.NAME,T1.AREA_ID=T2.A_ID ");
                    sb.append("WHEN NOT MATCHED THEN INSERT VALUES(T2.ID,T2.NAME,T2.A_ID)");
                    System.out.println("sql=" + sb.toString());
                    conn.setAutoCommit(false);
                    ps = conn.prepareStatement(sb.toString());
                    for (int i = 0; i < ol.size(); i++) {
                        Branch b = (Branch) ol.get(i);
                        ps.setString(1,b.getBranchId());
                        ps.setString(2,b.getBranchName());
                        ps.setString(3,b.getAreaId());
                        System.out.println(b.toString());
                        ps.addBatch();
                    }
                    ps.executeBatch();
                    conn.commit();

批量执行该MERGE语句



展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部