I'm trying to execute a SQL
query using a Java PreparedStatement
in Java 7 using the code below:
PreparedStatement functionalCRsStatement = con.prepareStatement(
"select * from openquery(SERVER,\n" +
"\t'Select X , Y, Z, A from D r\n" +
"\tINNER JOIN E c\n" +
"\tON r.RNID = c.RNID\n" +
"\twhere c.Y = ?')\n");
functionalCRsStatement.setString(2, x);
I get the following error message: com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range.
PS: I'm sure of the correctness of the SQL query because I successfully tested it without a PreparedStatement
, I just replaced the real name of the columns in the query by fake ones (X, Y, Z) to hide potentially confidential information.
EDIT: I get a similar error when using setString(1, x)
=> index 1 is out of range