I'm executing this query:
SELECT psq_id, question_text
FROM presales_question
WHERE psq_id IN(SELECT zz.psq_id FROM productservice_psq as zz WHERE zz.ps_id = 1)
When I execute it directly in SQL Server Management Studio, it works.
When I execute it with a standard Java Statement & Result Set, it works.
When I execute it using the Apache Commons DBUtils library (v 1.5), I get:
Exception in thread "main" java.sql.SQLException: com.microsoft.sqlserver.jdbc.SQLServerException: The multi-part identifier "zz.ps_id" could not be bound. Query: SELECT psq_id, question_text FROM presales_question WHERE psq_id IN(SELECT zz.psq_id FROM productservice_psq as zz WHERE zz.ps_id = ?) Parameters: [1]
at org.apache.commons.dbutils.AbstractQueryRunner.rethrow(AbstractQueryRunner.java:363)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:350)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:211)
It seems the problem is not with the query, but with the dbutils library. Digging into the source, I see the error is thrown when dbutils calls the getParameterMetaData()
method of the Statement. I'm still unsure how to fix this.
My interim solution has been to edit the source code of the dbutils library and disable the call to getParameterMetaData()
(see line 196 of AbstractQueryRunner.java). All it does is check that the param count passed in matches what's found in the query and throw an error if !=. I'm OK with skipping that check since the query will fail anyway if the params don't match.