问题 JDBC SQL Server:未为参数编号设置该值


我从从java代码调用存储过程的代码中收到以下错误:

异常跟踪{} org.springframework.jdbc.UncategorizedSQLException:   CallableStatementCallback;未分类SQL的SQLException [{call   test.usp_xxx_GetCompanyDetails(?,?,?,?,?,,?,,   ?,?,?,?,?)}]; SQL状态[null];错误代码[0];价值不是   设置参数编号11。嵌套异常是   com.microsoft.sqlserver.jdbc.SQLServerException:未设置该值   对于参数编号11. at   org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:84)   在   org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)   在   org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)   在   org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:1095)   在   org.springframework.jdbc.core.JdbcTemplate.call(JdbcTemplate.java:1131)

该应用程序部署在WAS 8.5.5上并使用jdbc驱动程序版本4.2。重新启动服务器时,此问题不再发生。生成的以下调用语句看起来不正确。连续逗号没有?它们之间。

{call test.usp_xxx_GetCompanyDetails(?,?,?,?,?,,?,,?,?,?,?,   ?)}

存储过程有10个参数。以下是存储过程的定义:

CREATE PROCEDURE [test].[usp_xxx_GetCompanyDetails]
(
    @ANumber        int,
    @CompanyId      int,
    @UserRole       varchar(15),
    @RequestId      varchar(100),
    @CompanyCode    varchar(5),
    @BaseSystem     varchar(5),
    @PType          varchar(20),    
    @PId            varchar(40),
    @IsActive       bit,     
    @responseData   xml OUT
)

以下是调用存储过程的java代码。它使用spring数据进行调用。

 private String executeProc(Integer aNumber,Integer companyId, String baseSystem, 
                String role,String companyCode,String requestId, String pType,String pId,
                boolean isActive ) throws SQLException {

            SQLXML responseData=null;
            Map<String,Object> inputParams= new HashMap<>();
            inputParams.put("ANumber", aNumber);
            inputParams.put("CompanyId", companyId);
            inputParams.put("UserRole", role);
            inputParams.put("RequestId", requestId);
            inputParams.put("CompanyCode", companyCode);
            inputParams.put("BaseSystem", baseSystem);
            inputParams.put("PType", pType);
            inputParams.put("PId", pId);
            inputParams.put("IsActive", isActive);
            inputParams.put("ResponseData", responseData);

            Map<String, Object> result = this.execute(inputParams);
            String responseXMLString = ((SQLXML) result.get("ResponseData")).getString();

            return responseXMLString;
        }

可能出了什么问题。


8183
2017-09-03 05:02


起源

删除不必要的逗号(,)以正确执行代码。 - Mostch Romi
该程序有10个参数。 SQL有12个逗号,因此它提供了13个参数,但只有11个 ? 标记,因此两个参数是未定义的。错误消息表明您没有为第11个标记设置值,即不调用 setXxx(11, xxx)。难怪JDBC驱动程序对您在此处尝试执行的操作感到困惑。 - Andreas
@Andreas,SQL是使用spring-data和hibernate框架自动生成的。服务器重启后,相同的代码工作。 - pawinder gupta
它看起来像是之间的一步 this.execute() 在代码示例中,以及 JdbcTemplate.call() 在堆栈?第一个参数 JdbcTemplate.call() 是一个 CallableStatementCreator,这通常是不可改变的 SimpleCallableStatementCreator 基于a String。听起来这可能与此有所不同? - df778899
@ df778899,重启服务器修复了问题。它可以链接到类加载器或加载的库。我无法复制这个问题。 - pawinder gupta


答案:


你有11个绑定参数(?)和空参数 , ,。将其移除以使用具有9个输入参数和1个输出参数的过程

{call test.usp_xxx_GetCompanyDetails(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}

7
2017-09-03 05:09



我差点投票,但程序有10个参数。该 OUT参数仍然是一个参数。 - Andreas
代码正在注册10个参数。 call语句由spring数据库生成。服务器重启后,相同的代码正在运行。 - pawinder gupta


你需要注册out参数,你可以尝试这样

cs.registerOutParameter("ResponseData", java.sql.Types.VARCHAR);

3
2017-09-12 12:40



代码是注册参数。 - pawinder gupta