gpt4 book ai didi

sql - H2 数据库 : expected "ALL, ANY, SOME, SELECT, FROM, WITH"

转载 作者:行者123 更新时间:2023-12-04 16:01:42 26 4
gpt4 key购买 nike

我使用 H2 作为内存数据库来对我们的应用程序进行单元测试。此查询给出错误 expected "ALL, ANY, SOME, SELECT, FROM, WITH" for this sql query

SELECT CF.*, ROWNUM CASHFLOW_INDEX
FROM ( SELECT * FROM
TB_CASHFLOWS CFW WHERE CFW.CASHFLOW_DATE >= :runDate AND
CFW.CASHFLOW_TYPE <> 'ISSUE' AND CFW.ISIN = :securityCode ORDER BY
CFW.CASHFLOW_DATE,CFW.CASHFLOW_TYPE ASC ) CF

在 runDate 位置如下:

SELECT CF.*, ROWNUM CASHFLOW_INDEX
FROM ( SELECT * FROM
TB_CASHFLOWS CFW WHERE CFW.CASHFLOW_DATE >= :[*]RUNDATE AND
CFW.CASHFLOW_TYPE <> 'ISSUE' AND CFW.ISIN = :SECURITYCODE ORDER BY
CFW.CASHFLOW_DATE,CFW.CASHFLOW_TYPE ASC ) CF

但当参数为“2017-02-28”和“USD_TREASURY_28FEB”时,相同的查询在 h2 控制台中运行良好。该查询将由 spring jdbc 模板执行。这是 H2 参数传递方式的问题吗?

java代码如下:

Object[] cashflowQueryArgs = new Object[] {"2017-02-28","USD_TREASURY_28FEB" };

List<Cashflow> instrumentCashflows = getJdbcTemplate().query(
cashflowsQuery,
cashflowQueryArgs,
new BeanPropertyRowMapper<Cashflow>(Cashflow.class));

最佳答案

幸运的是,我找到了解决方案。我认为 H2 不支持命名参数。所以我将命名参数更改为正常的问号并且它起作用了!

SELECT CF.*, ROWNUM CASHFLOW_INDEX
FROM ( SELECT * FROM
TB_CASHFLOWS CFW WHERE CFW.CASHFLOW_DATE >= ? AND
CFW.CASHFLOW_TYPE <> 'ISSUE' AND CFW.ISIN = ? ORDER BY
CFW.CASHFLOW_DATE,CFW.CASHFLOW_TYPE ASC ) CF

更新了命名参数。

对于 Oracle SQL,当将参数数组传递给查询方法时,spring jdbc 模板对于普通参数和命名参数都可以正常工作。但是对于 H2,我必须将命名参数模板与 SqlParameterSource 一起使用。示例如下:

MapSqlParameterSource cashflowQueryParamSource = new MapSqlParameterSource();
cashflowQueryParamSource.addValue("runDate", cashflowQueryArgs[0]);
cashflowQueryParamSource.addValue("securityCode", cashflowQueryArgs[1]);

List<Cashflow> instrumentCashflows = namedJdbcTemplate.query(cashflowsQuery, cashflowQueryParamSource,
new BeanPropertyRowMapper<Cashflow>(Cashflow.class));

希望这对您有所帮助。

关于sql - H2 数据库 : expected "ALL, ANY, SOME, SELECT, FROM, WITH",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50329189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com