简体   繁体   English

MsSql 中的存储过程期望输出参数作为输入

[英]Stored Procedure in MsSql expecting output parameter as input

I am calling a Stored Procedure from my Java code.我正在从我的 Java 代码调用存储过程。 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.正如您在这里看到的,我将 4 个参数传递给存储过程,第 5 个参数是输出。 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;一个OUTPUT参数仍然是一个输入参数; 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.您仍然需要向参数传递一个变量,因为一旦 SP 完成, OUTPUT参数的值将分配给该变量。

If you were using T-SQL to execute the SP, it would look like this:如果您使用 T-SQL 执行 SP,它将如下所示:

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 数据库<>小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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