MERGE JDBCTemplate问题

时间:2017-07-18 14:48:41

标签: spring jdbctemplate

我知道我的问题类似于这个问题:UPDATE on INSERT duplicate primary key in Oracle?

实际上是精确的副本。

我是Spring框架的新手。所以这是我的第一次尝试。我试过了很多例子,但却惨遭失败。

所以这就是事情。

我的源和目标是相同的。我想要的是更新记录,如果它在Db中否则插入。

我使用以下方法。我的插入查询工作正常。但是当我使用MERGE时。我收到了错误。

public void insertAllPointss(final List<AllPoints> allpoints) {


//      String sql = "INSERT INTO myTable " +
//                  "(WIFI_SEQ_NO, my_urlL, street_number, house_number, street_name, last_update) VALUES (seq_w.NEXTVAL, ?, ?, ?, ?, ?) ";

         String sql = "MERGE INTO myTable M" +
                  " USING (SELECT ? AS my_urlL, ? AS street_number, ? AS house_number,? AS street_name, ? AS last_update FROM dual) N"+
                    "  ON (M.street_number = N.street_number AND M.house_number = N.house_number AND M.street_name = N.street_name)"+
                  " WHEN MATCHED THEN UPDATE SET M.last_update = N.last_update"+
                  " WHEN NOT MATCHED THEN INSERT(WIFI_SEQ_NO, my_urlL, street_number, house_number, street_name, last_update)"+
                                       "  VALUES(seq_w.NEXTVAL, N.my_urlL, N.street_number, N.house_number, N.street_name, N.last_update)";

          getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {

              Calendar calendar = Calendar.getInstance();
              java.sql.Date ourJavaDateObject = new java.sql.Date(calendar.getTime().getTime());
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                AllPoints allpoints = AllPointss.get(i);
                ps.setString(1, AllPoints.getWebsite());
                ps.setDouble(2, allpoints.getStreetNumber());
                ps.setDouble(3, allpoints.getHouseNumber());
                ps.setString(4, allpoints.getStreetName());
                ps.setDate(5, ourJavaDateObject);
            }

            @Override
            public int getBatchSize() {
                return AllPointss.size();
            }
          });


        }

以下是我的日志文件

[7/18/17 8:24:00:952 CST] 00000043 com.ibm.ws.rsadapter.spi.DatabaseHelper                      I DSRA8203I: Database product name : H2
[7/18/17 8:24:00:952 CST] 00000042 com.ibm.ws.rsadapter.spi.DatabaseHelper                      I DSRA8203I: Database product name : H2
[7/18/17 8:24:00:952 CST] 00000043 com.ibm.ws.rsadapter.spi.DatabaseHelper                      I DSRA8204I: Database product version : 1.4.195 (2017-04-23)
[7/18/17 8:24:00:952 CST] 00000042 com.ibm.ws.rsadapter.spi.DatabaseHelper                      I DSRA8204I: Database product version : 1.4.195 (2017-04-23)
[7/18/17 8:24:00:952 CST] 00000043 com.ibm.ws.rsadapter.spi.DatabaseHelper                      I DSRA8205I: JDBC driver name  : H2 JDBC Driver
[7/18/17 8:24:00:952 CST] 00000042 com.ibm.ws.rsadapter.spi.DatabaseHelper                      I DSRA8205I: JDBC driver name  : H2 JDBC Driver
[7/18/17 8:24:00:952 CST] 00000043 com.ibm.ws.rsadapter.spi.DatabaseHelper                      I DSRA8206I: JDBC driver version  : 1.4.195 (2017-04-23)
[7/18/17 8:24:00:952 CST] 00000042 com.ibm.ws.rsadapter.spi.DatabaseHelper                      I DSRA8206I: JDBC driver version  : 1.4.195 (2017-04-23)
[7/18/17 8:24:01:108 CST] 00000042 SystemErr                                                    R Exception in thread "SimpleAsyncTaskExecutor-1" 
[7/18/17 8:24:01:108 CST] 00000043 SystemErr                                                    R Exception in thread "SimpleAsyncTaskExecutor-2" 
[7/18/17 8:24:01:108 CST] 00000043 SystemErr                                                    R org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [MERGE INTO myTable M USING (SELECT ? AS my_urlL, ? AS street_number, ? AS house_number,? AS street_name, ? AS last_update FROM dual) N  ON (M.street_number = N.street_number AND M.house_number = N.house_number AND M.street_name = N.street_name) WHEN MATCHED THEN UPDATE SET M.last_update = N.last_update WHEN NOT MATCHED THEN INSERT(WIFI_SEQ_NO, my_urlL, street_number, house_number, street_name, last_update) VALUES(seq_w.NEXTVAL, N.my_urlL, N.street_number, N.house_number, N.street_name, N.last_update)

我觉得我的MERGE SQL STATEMENT有问题,我正在寻找一些帮助/指导

1 个答案:

答案 0 :(得分:1)

使用正确的语法:

MERGE INTO people AS t 
USING (
    VALUES  (5, 'Chuck', 'Norris'),
                 (6, 'John', 'Smith'),
                 (7, 'Abraham', 'Lincoln')
                 -- maybe more rows
       ) AS s (id, name, surname)
    ON t.id = s.id
    WHEN MATCHED THEN
        UPDATE SET t.name=s.name, t.surname=s.surname
    WHEN NOT MATCHED THEN
        INSERT (id, name, surname)
              VALUES (s.id, s.name, s.surname)

Oracle Merge