对于输出参数访问,什么原因会导致SQL Server JDBC错误“没有为参数号0设置值”?

aij0ehis  于 2022-12-10  发布在  SQL Server
关注(0)|答案(5)|浏览(454)

I have some Java code that accesses SQL Server 2005 which looks something like this:

CallableStatement cstmt = ...;
... // Set input parameters
cstmt.registerOutParameter(11, Types.INTEGER);
cstmt.execute();
int out = cstmt.getInt(11);

And the following exception is thrown from the last line:

com.microsoft.sqlserver.jdbc.SQLServerException: The value is not set 
     for the parameter number 0.
   at com.microsoft.sqlserver.jdbc.SQLServerException.
     makeFromDriverError(Unknown Source)
   at com.microsoft.sqlserver.jdbc.SQLServerCallableStatement.
     skipOutParameters(Unknown Source)
   at com.microsoft.sqlserver.jdbc.SQLServerCallableStatement.
     getOutParameter(Unknown Source)
   at com.microsoft.sqlserver.jdbc.SQLServerCallableStatement.
     getterGetParam(Unknown Source)
   at com.microsoft.sqlserver.jdbc.SQLServerCallableStatement.
     getInt(Unknown Source)
   at org.jboss.resource.adapter.jdbc.WrappedCallableStatement.
     getInt(WrappedCallableStatement.java:192)

The stored procedure being called looks something like this:

CREATE PROCEDURE dbo.stored_proc ( -- 10 input parameters
                                 , @out_param INT OUTPUT) AS

-- Variable declarations 

SET @out_param = 0

-- Do processing...

SET @out_param = 1

Since the output parameter is set to zero on entry to the stored procedure, under what circumstances could the value not be set? Or am I misinterpreting the error message?

This error is reproducible with:

  • SQL Server JDBC Driver 1.2
  • SQL Server 2005 (64-bit) service pack 2
  • SQL Server 2005 (64-bit) service pack 3
    Update: It seems to be occuring as a result of the -- Do processing... part of the stored procedure. Removing this eliminates the error. There's too much code to reproduce here, what I'd like is some pointers to possible causes to narrow down likely candidates.
    Update: Injecting errors (e.g. divide by zero) into the -- Do processing... part of the stored procedure does not cause this exception to be thrown (instead, as expected, the execute() call fails with an appropriate error message).
    Update: Decompiling the com.microsoft.sqlserver.jdbc.SQLServerCallableStatement class suggests that 'parameter number 0' is the stored procedure return value.
    Update: I have been unable to reproduce this by calling the stored procedure directly through Management Studio.
    Update: The ultimate cause of this error seems to be a deadlock in the stored procedure. Usually, however, deadlocks cause the execute() call to fail with a SQLException wrapping SQL Server error code 1205...
flvlnr44

flvlnr441#

在你的参数上,你只是声明了它们还是把它们设置成了默认值?试着把它们设置成一个空的默认值或者别的什么,看看你是否仍然得到这个错误。
如果您有一个未设置为默认值的参数,并且在执行存储过程时未向其传递值,那么sql server将不喜欢这样做。

daupos2t

daupos2t2#

是否正在注册输出参数?根据文档“在执行存储过程之前必须注册所有OUT参数”。

5hcedyr0

5hcedyr03#

我想到了几件事:
1.你还没有得到一个RETURN x(其中x是一个INT代表处理的结果,0 =成功,其他任何东西代表警告或错误)。
1.除了输出参数之外,客户端代码没有为返回值分配参数。
希望这能有所帮助,
比尔

jchrr9hc

jchrr9hc4#

Decompiling the com.microsoft.sqlserver.jdbc.SQLServerCallableStatement class suggests that 'parameter number 0' is the stored procedure return value.
Thats correct. If your stored procedure definition has 11 input/output parameters, you actually need to define 12 in your calling code. Parameter 0 is the return code.
For example with a SP with one input and one output parameter, in SSMS you run this:

DECLARE @ReturnValue INT
DECLARE @P1 INT, @P2 INT

EXEC @ReturnValue= YourSP(@P1,@P2 OUTPUT)

You actually have three parameters to deal with here, not two.

omjgkv6w

omjgkv6w5#

如果任何人仍然得到这个问题的SQL Server 2008.
我在试图为DateTime字段设置时间戳的update语句中遇到了同样的问题。出错的字段位于where子句中,其索引与报告的索引不同。

相关问题