MS SQL and Java: cannot find table variable during call stored procedure

时间:2016-11-09 08:18:33

标签: java sql-server table-variable

I'm trying to execute MS-SQL stored procedure from Java.

One of the procedure parameters is a table variable (in this case, it is called [Items]). The procedure and the table variable defined in a separate schema (in this case is [test]).

During call the procedure, the following error occurs "com.microsoft.sqlserver.jdbc.SQLServerException: Column, parameter, or variable @P1. : Cannot find data type Items.".

I used Spring's SimpleJdbcCall and Microsoft's jdbc driver - sqljdbc-6.0 to work with MS-SQL:

SimpleJdbcCall testCall = new SimpleJdbcCall(jdbcTemplate)
        .withSchemaName("[test]")
        .withProcedureName("[SomeTestProcedure]")
        .withoutProcedureColumnMetaDataAccess()
        .declareParameters(
                new SqlParameter("Id", Types.INTEGER),
                new SqlParameter("Items", microsoft.sql.Types.STRUCTURED));
testCall.compile();
SQLServerDataTable sdt;
try {
    sdt = new SQLServerDataTable();
    sdt.addColumnMetadata("Name", Types.NVARCHAR);
    sdt.addColumnMetadata("Value", Types.INTEGER);
    sdt.addRow("DEV", 1);
    sdt.addRow("PROD", 2);
} catch (SQLServerException e) {
    throw new IllegalStateException(e);
}
testCall.execute(1, sdt);

I would be grateful for help in solving the issue. Thanks.

1 个答案:

答案 0 :(得分:0)

As I found out, the driver tries to use a table variable from the default schema that is set to the user.

In my case, the [dbo]-scheme was set as a default schema. Accordingly, [dbo]-scheme has no this table variable [Items], that caused the error "Cannot find data type Items". After we changed the default schema of the user to [test] (in which the table variable was created), the error disappeared.