views:

69

answers:

1

Hello, I have a stored proc in SQL Server 2005, which looks like the following (simplified)

CREATE PROCEDURE FOO
 @PARAMS
AS
BEGIN
  -- STEP 1: POPULATE tmp_table
  DECLARE @tmp_table TABLE (...)
  INSERT INTO @tmp_table
      SELECT * FROM BAR

  -- STEP 2: USE @tmp_table FOR FINAL SELECT
  SELECT abc, pqr
    FROM BAZ JOIN @tmp_table 
    ON some_criteria
END

When I run this proc from SQL Server Management Studio, things work fine. However, when I call the same proc from a Java program, using something like:

cs = connection.prepareCall("exec proc ?,");
cs.setParam(...);
rs = cs.getResultSet(); // BOOM - Null!

while(rs.next()) {...} // NPE!

I fail to understand why the first result set returned is NULL. Can someone explain this to me?

As a workaround, if I check cs.getMoreResults() and if true, try another getResultSet() - THIS time it returns the proper result set.

Any pointers please? (I'm using JTDS drivers, if it matters)

Thanks, Raj

+1  A: 

The Javadoc for getResultSet() says that it returns null "... if the result is an update count or there are no more results". It looks like your stored procedure would have an update count and a resultset, and that the getResultSet() method is (arguably) just doing what the API contract says it should do.

You could try retrieving the update count first. Otherwise, stick with your "workaround".

Stephen C
Yes. Also one thing I noticed is that "executeQuery()" seems to work all the time (never returns null).Accepting your answer anyway...
Raj
@Raj - presumably if you execute using executeQuery there is no way to retrieve the update count. (Question - is the update count from the INSERT actually retrievable?)
Stephen C