2
votes

I am trying to execute following SQL with Spring JdbcTemplate:

INSERT INTO japan_wht.PIVOT_20427002(doc_header_text, value_date, total_amt, is_refund)                    
 (SELECT 
    doc_header_text, DATE(value_date), SUM(LOCAL_CCY_AMT), is_refund
 FROM
    (SELECT 
        *
    FROM
        japan_wht.DATA_20427002
    WHERE IS_REFUND in ('N')
    ) t 
GROUP BY DATE(value_date) , doc_header_text, is_refund)

However, it does not insert anything into database table and no error is thrown.

When I tried to execute following SQL with JdbcTemplate, it works and inserts data in DB table:

INSERT INTO japan_wht.PIVOT_20427002(id, doc_header_text, value_date, total_amt, is_refund) values('1', '1', '2017-12-31', 3000, 'Y');

Below is my call to execute above SQLs:

jdbcTemplate.update(sqlString);

Not sure what is going wrong here.

2
can show us the sqlString please? - YCF_L
What kind of exception do you get? Can you run this SELECT INTO manually? - Tamas Rev
@YCF_L: sqlString is the SQLs I have given in question... - Akshay Lokur
@Tamas Rev: I am not getting any exception. Just data is not getting inserted. Select statement when run manually on its own does return data... - Akshay Lokur
Run the select statement in a DB client program (SqlSquirrel, DB Visualizer etc) and see if it returns any rows. - dsp_user

2 Answers

1
votes

I had to resort to plain JDBC and it worked:

Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/mySchema?autoReconnect=true&useSSL=false&rewriteBatchedStatements=true",
    "root", "root");
Statement stmt = conn.createStatement();
int flag = stmt.executeUpdate(sqlString);
LOGGER.info("Flag = {}", flag);

Not sure why Spring JdbcTemplate can not handle such thing!

0
votes

You can use the execute method. Like:

jdbcTemplate.execute("INSERT INTO table1 (some_id, some_key, some_text) " +
      "SELECT id, key, text from table2 t2 " +
      "LIMIT ?", (PreparedStatementCallback<Boolean>) ps -> {
                      ps.setInt(1, 400);
                      return ps.execute();
                  });