views:

1771

answers:

3

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...

A: 

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.

DForck42
Is this true of OUTPUT parameters as well?
Matthew Murdoch
haven't a clue. i'm not entirely sure i understand what you mean by output parameters
DForck42
Unless I'm missing something, "The value is not set for the parameter number 0.", is most likely refering to an input parameter, that has not been set?
John Sansom
@Jonn Sansom - the error message is confusing and my original assumption was that it referred to input parameters. However, the line number of the application code from where the exception is thrown (not shown in the stack trace above) is the getInt() output parameter access call.
Matthew Murdoch
@Thirster42 - in this case output parameters are those set by the stored procedure rather than the Java code (i.e. those registered with registerOutParameter() in the Java code and defined as OUTPUT in the stored procedure declaration).
Matthew Murdoch
hmm... i don't know about java or jdbc, returning more than one result set (ie, returning 2 variables) breaks in SSRS (or rather, it just selects the first value). Maybe something similar is happening?
DForck42
@Thirser42 - tried setting the output parameters to a default value. This is allowed when defining the stored procedure but the default value is ignored on execution.
Matthew Murdoch
A: 

Are you registering the output parameters? According the the docs "All OUT parameters must be registered before a stored procedure is executed."

Vincent Ramdhanie
Yes, they're registered. Will update the code.
Matthew Murdoch
A: 

Hi,

There are a couple things that come to mind:

  1. You haven't got a RETURN x (where x is an INT representing the outcome of processing, 0 = success, anything else represent a warning or an error).
  2. Your client code doesn't assign a parameter for the return value in addition to your output parameter.

Hope this helps,

Bill

Bill Mueller