Skip to content

UPDATE SQL clause preparedstatement parsing does not always work correctly #949

@vtuonone

Description

@vtuonone

Driver version

7.2.0 jre11

SQL Server version

Any

Client Operating System

Any Windows

JAVA/JVM version

OpenJDK 11

Problem description

For some reason only fist prepared statement query is parsed correctly
UPDATE TxCounter SET addedCnt=addedCnt+1 OUTPUT INSERTED.addedCnt WHERE code=:S1 AND (cnt=-1 OR cnt=-2 OR addedCnt<cnt)
and
UPDATE TxCounter SET addedCnt=addedCnt+1 OUTPUT INSERTED.addedCnt WHERE (cnt=-1 OR cnt=-2 OR addedCnt<cnt) AND code=:S1

  1. Expected behaviour: Both queries are parsed and works equal
  2. Actual behaviour: First prepared statement parse does not find parameters at all
  3. Error message/stack trace:
    While trying to set parameter value following error has been thrown
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The index 1 is out of range.
	at com.microsoft.sqlserver//com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:228)
	at com.microsoft.sqlserver//com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setterGetParam(SQLServerPreparedStatement.java:1124)
	at com.microsoft.sqlserver//com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setValue(SQLServerPreparedStatement.java:1138)
	at com.microsoft.sqlserver//com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setString(SQLServerPreparedStatement.java:1683)

PS: Both one works nicely in 7.1.4

Reproduction code

PreparedStatement st1 = conn.prepareStatement(
                  "UPDATE TxCounter SET addedCnt=addedCnt+1 OUTPUT INSERTED.addedCnt WHERE code=? AND (cnt=-1 OR cnt=-2 OR addedCnt<cnt)");
PreparedStatement st2 = conn.prepareStatement(
                  "UPDATE TxCounter SET addedCnt=addedCnt+1 OUTPUT INSERTED.addedCnt WHERE (cnt=-1 OR cnt=-2 OR addedCnt<cnt) AND code=?");
st1.setString(1, "x");
//This one fails
st2.setString(1, "x");

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in the driver. A high priority item that one can expect to be addressed quickly.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions