When an INSERT statement contains a subquery with parameters, they are not included in the count returned by PreparedStatement.getParameterMetaData().getParameterCount().
Example:
Create a table: CREATE TABLE EXPERIMENT (ID NUMERIC(9,0));
Run the following test, given an established Connection conn:
@Test
public void parameterCountForInsertWithSubquery() throws SQLException {
try (PreparedStatement ps = conn.prepareStatement(
"insert into experiment (id) select ? where not exists (select * from experiment where id = ?)");
) {
Assert.assertEquals(2, ps.getParameterMetaData().getParameterCount());
}
}
Output:
java.lang.AssertionError:
Expected :2
Actual :1
Please note that the query actually executes fine if you set the two parameters on ps and call executeUpdate().
Tested with driver 6.1.0.jre8 and also a local build of 8103dc3 against Microsoft SQL Server 2008 R2 (SP2) - 10.50.4000.0 (X64)
When an INSERT statement contains a subquery with parameters, they are not included in the count returned by
PreparedStatement.getParameterMetaData().getParameterCount().Example:
Create a table:
CREATE TABLE EXPERIMENT (ID NUMERIC(9,0));Run the following test, given an established
Connection conn:Output:
Please note that the query actually executes fine if you set the two parameters on
psand callexecuteUpdate().Tested with driver 6.1.0.jre8 and also a local build of 8103dc3 against Microsoft SQL Server 2008 R2 (SP2) - 10.50.4000.0 (X64)