[英]Stored procedure does not output result
As a part of getting to learn Stored procedures
, I came up with this. 作为学习
Stored procedures
,我想到了这一点。
CREATE PROCEDURE StoredProcedure1
@oResult int output
AS
BEGIN
SELECT @oResult = 2
RETURN @oResult
END
Procedure or function 'StoredProcedure1' expects parameter '@oResult', which was not supplied.
I want this procedure just to return a result when called. Any ideas why ? 有什么想法吗?
EXEC StoredProcedure1 @oResult = 0
but it just says Command Completed Successfully
but no output. EXEC StoredProcedure1 @oResult = 0
但它只是说Command Completed Successfully
但没有输出。 Any ideas why ? 有什么想法吗?
In ADO.NET when you call a Stored Procedure that expects a parameter, you need to give that parameter, also if it is an output parameter. 在ADO.NET中,当您调用需要参数的存储过程时,也需要提供该参数,即使它是输出参数。
using(SqlConnection cnn = new SqlConnection(.....))
using(SqlCommand cmd = new SqlCommand("StoredProcedure1", cnn))
{
cnn.Open();
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p = new SqlParameter("@oResult", SqlDbType.Int);
p.Direction = ParameterDirection.Output;
cmd.Parameters.Add(p);
cmd.ExecuteNonQuery();
int result = Convert.ToInt32(cmd.Parameters["@oResult"].Value);
}
Of course, in the stored procedure you should set the @oResult parameter in some way as explained in the other answers, but if you use an OUTPUT parameter there is no need to RETURN the same value. 当然,在存储过程中,应按照其他答案中所述的方式设置@oResult参数,但是如果使用OUTPUT参数,则无需返回相同的值。
However you could have both an OUTPUT parameter and a RETURN value if you need to. 但是,如果需要,可以同时具有OUTPUT参数和RETURN值。 In this case your call from C# should be
在这种情况下,您从C#拨打的电话应为
using(SqlConnection cnn = new SqlConnection(.....))
using(SqlCommand cmd = new SqlCommand("StoredProcedure1", cnn))
{
cnn.Open();
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p = new SqlParameter("@oResult", SqlDbType.Int);
p.Direction = ParameterDirection.Output;
cmd.Parameters.Add(p);
// Here the name @returnValue is arbitrary, you could call it whatever you like
SqlParameter r = new SqlParameter("@returnValue", SqlDbType.Int);
r.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(r);
cmd.ExecuteNonQuery();
int result = Convert.ToInt32(cmd.Parameters["@oResult"].Value);
int returnValue = Convert.ToInt32(cmd.Parameters["@returnValue"].Value);
}
But keep in mind that RETURN values are limited to Integer Expressions . 但是请记住, RETURN值仅限于整数表达式 。
You could do this: 您可以这样做:
Store procedure 存放程序
CREATE PROCEDURE StoredProcedure1
@oResult int output
AS
BEGIN
SET @oResult = 2
END
And then calling it like this: 然后像这样调用它:
DECLARE @Result INT
exec StoredProcedure1 @oResult = @Result output
SELECT @Result
This will output 这将输出
2
Update: 更新:
Like addressed in the comment. 喜欢在评论中解决。 You could also simplify the statement.
您还可以简化该语句。 By doing this:
通过做这个:
DECLARE @Result INT
exec StoredProcedure1 @Result output
SELECT @Result
Reference: 参考:
You do not need to write return ..try below code :- 您无需在以下代码中编写return ..try:-
CREATE PROCEDURE StoredProcedure1
@oResult int output
AS
BEGIN
SET @oResult = 2
END
CREATE PROCEDURE SP1
(
@oResult int output
)
AS
BEGIN
SET @oResult=2
Select @oResult
END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.