Unable to execute stored Procedure using Java and

2019-06-04 02:15发布

问题:

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;
    DECLARE @OutIdentifier int;
    EXEC @return_value = [dbo].[spWCoTaskIdGen] @OutIdentifier = @OutIdentifier OUTPUT;
    SELECT
        @OutIdentifier [@OutIdentifier],
        @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.

回答1:

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"



回答2:

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.



回答3:

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?



回答4:

Have you tried changing your Java code to use "exec" instead of "call"? That is:

CallableStatement statement = connection
                    .prepareCall("{exec dbo.spWCoTaskIdGen(?)}");