我正在学习 Oracle 12 C 的新功能(Top-N 查询和分页)。我尝试了一些带有 offset 和 fetch 关键字的示例,当我使用 sql Developer 时效果很好。我尝试使用 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 "$2"
我在 postgres 数据库上尝试了一些相同的操作,但没有运气,最后我想到删除参数,当我删除第三个参数并在查询字符串中设置硬编码值时,它起作用了:
"SELECT * FROM ENTITY E order by E.ID offset ? rows fetch next 10 rows only"
fetch子句中是否不允许使用bind参数?
最佳答案
如果在 fetch 参数上添加 (),则可以正常工作:
String query = "SELECT * FROM ENTITY E order by E.ID offset ? rows fetch next (?) rows only";
关于java - Spring JDBC模板未绑定(bind)所有参数(获取参数值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39612677/