views:

1003

answers:

3

I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I use when setting up the stored procedure call in code I get an error stating that the stored procedure couldn't be found. I have provided the stored procedure I'm executing below (NOTE: this is vendor code, so I cannot change it).

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO


ALTER PROC [dbo].[spWCoTaskIdGen] 
@OutIdentifier int OUTPUT
AS

BEGIN
DECLARE @HoldPolicyId int
DECLARE @PolicyId char(14)

IF NOT EXISTS
(
SELECT *
FROM UniqueIdentifierGen (UPDLOCK)
)
INSERT INTO UniqueIdentifierGen VALUES (0)

UPDATE UniqueIdentifierGen 
SET 
 CurIdentifier = CurIdentifier + 1

SELECT @OutIdentifier = 
 (SELECT CurIdentifier
 FROM UniqueIdentifierGen)
END

The code looks like:

 CallableStatement statement = connection
   .prepareCall("{call dbo.spWCoTaskIdGen(?)}");
 statement.setInt(1, 0);
 ResultSet result = statement.executeQuery();

I get the following error: SEVERE: Could not find stored procedure 'dbo.spWCoTaskIdGen'.

I have also tried

 CallableStatement statement = connection
   .prepareCall("{? = call dbo.spWCoTaskIdGen(?)}");
 statement.registerOutParameter(1, java.sql.Types.INTEGER);
 statement.registerOutParameter(2, java.sql.Types.INTEGER);
 statement.executeQuery();

The above results in: SEVERE: Could not find stored procedure 'dbo.spWCoTaskIdGen'.

I have also tried:

 CallableStatement statement = connection
   .prepareCall("{? = call spWCoTaskIdGen(?)}");
 statement.registerOutParameter(1, java.sql.Types.INTEGER);
 statement.registerOutParameter(2, java.sql.Types.INTEGER);
 statement.executeQuery();

The code above resulted in the following error: Could not find stored procedure 'spWCoTaskIdGen'.

Finally, I should also point out the following:

  1. I have used the MS SQL Server Management Studio tool and have been able to successfully run the stored procedure. The sql generated to execute the stored procedure is provided below:

    GO

    DECLARE @return_value int, @OutIdentifier int

    EXEC @return_value = [dbo].[spWCoTaskIdGen] @OutIdentifier = @OutIdentifier OUTPUT

    SELECT @OutIdentifier as N'@OutIdentifier '

    SELECT 'Return Value' = @return_value

    GO

  2. The code being executed runs with the same user id that was used in point #1 above.

  3. In the code that creates the Connection object I log which database I'm connecting to and the code is connecting to the correct database.

Any ideas?

Thank you very much in advance.

A: 

Since it can't even find the procedure, I would first look to make sure that your user has execute privileges for that procedure. You could try executing the proc with the same user from a tool like Squirrel.

Jason Gritman
I have done this and have provided the resulting SQL in the original question. Thank you for the suggestion.
jwmajors81
Squirrel will connect through JDBC, so if you try to run it through that you could verify that the driver/JDBC connection string isn't a problem. You can download it from here: http://squirrel-sql.sourceforge.net/
Jason Gritman
If you can't execute through Squirrel, it's probably more than likely your connection string rather than the driver. Make sure it looks like jdbc:sqlserver://localhost:1433;databaseName=ADatabase and that the databaseName is the correct name.
Jason Gritman
A: 

Try it without the dbo. owner designation:

CallableStatement statement = connection.prepareCall("? = spWCoTaskIdGen(?)");
statement.registerOutParameter(1, java.sql.Types.INTEGER);
statement.registerOutParameter(2, java.sql.Types.INTEGER);
statement.executeQuery();

Also, and this is a longshot, are you sure you're in the correct database within the database server?

Asaph
I have tried this and received the same error message. That example has been added to the original question. Thanks.
jwmajors81
+1  A: 

Most likely one of...

  1. The credentials uses have no rights to run the code. You'd need a GRANT EXECUTE in the script above

  2. Wrong database. For example, the stored proc was created in master but you are connected to "MyDB"

gbn
I have confirmed that the user does have grant privileges and that I am connected to the correct database. Thank you for the suggestion though.
jwmajors81