I'm executing the following query using NamedParameterJdbcTemplate
with a single parameter.
DDL For Table
create table TEST_TRANS
(
DESCRIPTION_2 float,
AMOUNT_STR varchar(255),
DESCRIPTION varchar(255)
)
SQL
UPDATE TEST_TRANS
SET DESCRIPTION_2 = CAST(AMOUNT_STR as float) / 100
WHERE
DESCRIPTION != :DESCRIPTION
When I execute the query using
Objects.requireNonNull(getNamedParameterJdbcTemplate())
.update(testQuery, Collections.singletonMap("DESCRIPTION","AED"));
I'm getting the following error.
Exception in thread "main" org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL [UPDATE TEST_TRANS SET DESCRIPTION_2 = CAST(AMOUNT_STR as float)/100 WHERE DESCRIPTION != ?]; The index 1 is out of range.; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: The index 1 is out of range.
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:110)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1442)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:632)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:861)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:882)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:320)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:325)
at com.seenukarthi.so.jdbctest.repository.TestJdbcRepository.test(TestJdbcRepository.java:32)
at com.seenukarthi.so.jdbctest.service.TestJdbcService.testService(TestJdbcService.java:19)
at com.seenukarthi.so.jdbctest.Main.main(Main.java:12)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The index 1 is out of range.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:228)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setterGetParam(SQLServerPreparedStatement.java:1124)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setValue(SQLServerPreparedStatement.java:1138)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setString(SQLServerPreparedStatement.java:1683)
at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:400)
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:232)
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:147)
at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.setValues(PreparedStatementCreatorFactory.java:286)
at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.createPreparedStatement(PreparedStatementCreatorFactory.java:244)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:614)
... 7 more
If I remove the /100
for the SQL everything is working. Also if I hardcode the parameter it's working
Environment:
Reproducer can be found in GitHub
After some research I found that the issue was with the JDBC driver mssql-jdbc version 7.2.0. After upgrading the driver to the latest version the issue is resolved
BUG Report for GitHub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With