简体   繁体   中英

Stored Procedure in MsSql expecting output parameter as input

I am calling a Stored Procedure from my Java code. And after some computation in the stored procedure, I am returning the status. Below is the stored procedure:

ALTER   PROCEDURE [dbo].[SAMPLE_ENTRIES](@ID INTEGER, @PERIOD_ID INTEGER, @RUN_ID INTEGER, @TYPE CHAR, @RESULT_OUTPUT varchar(100) OUTPUT)
/**Here goes some computation**/
IF @RESULT_OUTPUT IS NULL  
BEGIN   
   set @RESULT_OUTPUT = 'SUCCESS';    
   select @RESULT_OUTPUT as RESULT_OUTPUT;  
END; 

END

As you can see here I am passing 4 parameters to the stored procedure and the 5th parameter is the output. But I am getting an error, below is the error:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Procedure or function 'SAMPLE_ENTRIES' expects parameter '@RESULT_OUTPUT', which was not supplied.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:404)

I am new to Stored Procedures.

Thanks for the help in advance.

A OUTPUT parameter is still an input parameter; there is no such thing as a "non-input" parameter with Stored Procedures. You still need to pass a variable to the parameter, as the value of the OUTPUT parameter will be assigned to that variable once the SP completes.

If you were using T-SQL to execute the SP, it would look like this:

CREATE PROC dbo.MyProc @MyParam int OUTPUT AS
BEGIN

    SET @MyParam = 1;
END;
GO

DECLARE @MyParam int;
EXEC dbo.MyProc @MyParam OUTPUT;

PRINT @MyParam; --1

DB<>Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM