Oracle查询中的H2加/减间隔秒
Posted
技术标签:
【中文标题】Oracle查询中的H2加/减间隔秒【英文标题】:H2 add/subtract Interval Second in Oracle Query 【发布时间】:2018-01-16 14:16:56 【问题描述】:我在服务器模式下拥有用于测试的 h2 数据库。
当我的请求在 SQLDeveloper 中工作时,我无法在我的 spring-batch 编写器中为我的日期添加/减去一个间隔
h2 配置:
private DataSource createH2DataSource()
String jdbcUrl = String.format("jdbc:h2:%s/target/db/h2;AUTO_SERVER=TRUE;MODE=Oracle", System.getProperty("user.dir"));
JdbcDataSource ds = new JdbcDataSource();
ds.setURL(jdbcUrl);
ds.setUser("sa");
ds.setPassword("");
return ds;
液基:
<column name="DATE_DEBUT_BI" type="TIMESTAMP" />
<column name="DATE_FIN_BI" type="TIMESTAMP" />
我的要求:
UPDATE MY_TABLE SET DATE_FIN_BI = :dateFinBi - INTERVAL '1' SECOND WHERE DATE_J = :dateJ AND CODE_EDE = :codeEde AND STATUT_EFFACEMENT = :statutEffacement
错误:
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [UPDATE MY_TABLE SET DATE_FIN_BI = ? - INTERVAL '1' SECOND WHERE DATE_J = ? AND CODE_EDE = ? AND STATUT_EFFACEMENT = ?]; nested exception is org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "UPDATE MY_TABLE SET DATE_FIN_BI = ? - INTERVAL '1'[*] SECOND WHERE DATE_J = ? AND CODE_EDE = ? AND STATUT_EFFACEMENT = ?"; SQL statement:
UPDATE MY_TABLE SET DATE_FIN_BI = ? - INTERVAL '1' SECOND WHERE DATE_J = ? AND CODE_EDE = ? AND STATUT_EFFACEMENT = ? [42000-196]
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:649)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:662)
at org.springframework.jdbc.core.JdbcTemplate.batchUpdate(JdbcTemplate.java:950)
at org.springframework.jdbc.core.namedparam.NamedParameterBatchUpdateUtils.executeBatchUpdateWithNamedParameters(NamedParameterBatchUpdateUtils.java:40)
...
Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "UPDATE MY_TABLE SET DATE_FIN_BI = ? - INTERVAL '1'[*] SECOND WHERE DATE_J = ? AND CODE_EDE = ? AND STATUT_EFFACEMENT = ?"; SQL statement:
UPDATE MY_TABLE SET DATE_FIN_BI = ? - INTERVAL '1' SECOND WHERE DATE_J = ? AND CODE_EDE = ? AND STATUT_EFFACEMENT = ? [42000-196]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.message.DbException.getSyntaxError(DbException.java:191)
at org.h2.command.Parser.getSyntaxError(Parser.java:534)
at org.h2.command.Parser.prepareCommand(Parser.java:261)
at org.h2.engine.Session.prepareLocal(Session.java:578)
at org.h2.engine.Session.prepareCommand(Session.java:519)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1204)
at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:73)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:288)
at org.springframework.jdbc.core.JdbcTemplate$SimplePreparedStatementCreator.createPreparedStatement(JdbcTemplate.java:1524)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:627)
... 36 more
“解决方案”:
谢谢大家,所以我在java中提取了减法来进行简单的查询。
【问题讨论】:
不应该是1 SECOND
而不是'1' SECOND
吗?
@JoopEggen 没有''的同样错误
@STaefi [*]
是错误点的标记。
INTERVAL '1 SECOND'
我好像在什么地方看到了。
@JoopEggen 同样的事情:(
【参考方案1】:
最后我在 java 中提取了减法来进行简单的查询。 为此,我使用了 JdbcItemWriter 中的 ItemSqlParameterSourceProvider
ItemSqlParameterSourceProvider<NebefEffEdePdh> itemSqlParameter = nebefEffEdePdh -> new MapSqlParameterSource()
.addValue("dateDebutBiMoins1Second",Utils.getDateMinusSeconds(nebefEffEdePdh.getDateDebutBi(), DELTA_DATE_DEBUT_FIN_BI))
.addValue("dateJ", nebefEffEdePdh.getDateJ())
.addValue("codeEde", nebefEffEdePdh.getCodeEde())
.addValue("statutEffacement", nebefEffEdePdh.getStatutEffacement());
writer.setItemSqlParameterSourceProvider(itemSqlParameter);
我的要求:
UPDATE MY_TABLE SET DATE_FIN_BI = :dateDebutBiMoins1Second WHERE TRUNC(DATE_J) = TRUNC(:dateJ) AND CODE_EDE = :codeEde AND STATUT_EFFACEMENT = :statutEffacement AND DATE_FIN_BI IS NULL
【讨论】:
以上是关于Oracle查询中的H2加/减间隔秒的主要内容,如果未能解决你的问题,请参考以下文章