javasql-serverjdbcuser-defined-functionsapache-commons-dbutils

How to call a SQL Scalar function using Apache DbUtils


I am trying to call a SQLServer stored Scalar function through Apache Common DbUtils. I tried something like this:

run.query("SELECT [MyDB].[dbo].[test] ('testParam')", new ScalarHandler());

But, I get this exception:

com.microsoft.sqlserver.jdbc.SQLServerException: com.microsoft.sqlserver.jdbc.SQLServerException: Unable to identify the table SELECT [MyDB].[dbo].[test] ('testParam') for the metadata.

While, running the same query in the SQLServer returns a valid Scalar value.

I am wondering how can I call a Scalar function using Apache DbUtils.

UPDATE: For Table-valued functions it works if I ues "SELECT * FROM..."


Solution

  • It looks that there is a bug in Apache Common DbUtils (v. 1.5) AbstractQueryRunner (base class of QueryBuilder) code. It has parameter pmdKnownBroken, which desribed as follows:

    if pmdKnownBroken is set to true, we won't even try it; if false, we'll try it, and if it breaks, we'll remember not to use it again.

    But does not work as described, becausefillStatementmethod does not catch an exception when calling java.sql.getParameterMetaData.

    So you should set pmdKnownBroken to true.