Skip to content

Incorrect parameter count for INSERT with subquery #343

@vobarian

Description

@vobarian

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)

Metadata

Metadata

Assignees

Labels

Waiting for ResponseWaiting for a reply from the original poster, or affiliated party

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions