0

I know my questions is similar to this question : UPDATE on INSERT duplicate primary key in Oracle?

actually exact copy.

I am new to spring framework. so this is my first attempt. I have tried ,many examples but failed miserably.

So here is the thing.

My source and target is the same.what i want is to update the record if its in the Db otherwise insert.

i am using the following method. My insert query is working fine. But when i use MERGE. i am getting errors.

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


        }

following is my log file

[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)

i feel there is a problem my MERGE SQL STATEMENT, i am looking for some help/guidance

Harry
  • 27
  • 1
  • 8

1 Answers1

1

Correct Syntax use:

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

Bir Virk
  • 65
  • 6