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:
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...
on your parameters, are you just declaring them or are you setting them to a default value? try setting them to a default value of null or something and see if you still get the error.
sql server doesn't like it if you have a parameter not set to a default value and you don't pass a value to it when you exec the stored procedure.