Spring JDBC 模板未绑定所有参数(获取参数值)
Spring JDBC Template Not Binding all parameters (Fetch Parameter value)
我正在学习 Oracle 12 C 的新特性(Top-N 查询和分页)。我尝试了几个带有 offset 和 fetch 关键字的示例,当我使用 sql 开发人员时它运行良好。我尝试使用 Spring jdbcTemplate 实现相同的功能,但我看到了一个奇怪的行为。
例如:这是代码片段:
// get first ten entities
String query = "SELECT * FROM ENTITY E order by E.ID offset ? rows fetch next ? rows only";
SqlRowSet rowSet = this.jdbcTemplate.queryForRowSet(query, 1, 10);
while(rowSet.next()){
//do something
}
Spring 抛出以下异常:
org.springframework.jdbc.BadSqlGrammarException:
PreparedStatementCallback; bad SQL grammar [SELECT * FROM ENTITY E
order by E.ID offset ? rows fetch next ? rows only]; nested exception
is : ERROR: syntax error at or near ""
我在 postgres 数据库上尝试了一些相同的操作,但没有成功,最后我想到了删除参数,当我删除第三个参数并在查询中设置硬编码值时它起作用了 string.ex:
"SELECT * FROM ENTITY E order by E.ID offset ? rows fetch next 10 rows only"
是否不允许在fetch子句中使用绑定参数?
如果在 fetch 参数上加上 () 就可以了:
String query = "SELECT * FROM ENTITY E order by E.ID offset ? rows fetch next (?) rows only";
我正在学习 Oracle 12 C 的新特性(Top-N 查询和分页)。我尝试了几个带有 offset 和 fetch 关键字的示例,当我使用 sql 开发人员时它运行良好。我尝试使用 Spring jdbcTemplate 实现相同的功能,但我看到了一个奇怪的行为。 例如:这是代码片段:
// get first ten entities
String query = "SELECT * FROM ENTITY E order by E.ID offset ? rows fetch next ? rows only";
SqlRowSet rowSet = this.jdbcTemplate.queryForRowSet(query, 1, 10);
while(rowSet.next()){
//do something
}
Spring 抛出以下异常:
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT * FROM ENTITY E order by E.ID offset ? rows fetch next ? rows only]; nested exception is : ERROR: syntax error at or near ""
我在 postgres 数据库上尝试了一些相同的操作,但没有成功,最后我想到了删除参数,当我删除第三个参数并在查询中设置硬编码值时它起作用了 string.ex:
"SELECT * FROM ENTITY E order by E.ID offset ? rows fetch next 10 rows only"
是否不允许在fetch子句中使用绑定参数?
如果在 fetch 参数上加上 () 就可以了:
String query = "SELECT * FROM ENTITY E order by E.ID offset ? rows fetch next (?) rows only";