2014-10-08 65 views
0

作为学习的一部分Stored procedures,我想出了这个。存储过程不输出结果

CREATE PROCEDURE StoredProcedure1 
@oResult int output 
AS 
BEGIN 
    SELECT @oResult = 2 
    RETURN @oResult 
END 
  1. 但问题是,这个SP的执行,它只是返回的东西等。 Procedure or function 'StoredProcedure1' expects parameter '@oResult', which was not supplied. 我想这个过程只是为了在调用时返回一个结果。

任何想法为什么?

  1. 我按要求提供,EXEC StoredProcedure1 @oResult = 0但它只是说Command Completed Successfully但没有输出。

任何想法为什么?

+0

您能否显示调用此过程的C#代码?问题在那里。 – Steve 2014-10-08 06:49:54

+0

@Steve:我没有写任何C#代码史蒂夫。我只是写了这个来学习'存储过程'。这只是一个'mssql sp'的权利? C#只是调用这个权利? – 2014-10-08 06:51:23

+0

尝试选择@ oResult而不是@ @Result – artm 2014-10-08 06:51:49

回答

0

你并不需要写回..try下面的代码 -

CREATE PROCEDURE StoredProcedure1 
@oResult int output 
AS 
BEGIN 
    SET @oResult = 2 
END 
+0

谢谢。但同样,它期望我强制给出一个参数,并且它不显示任何结果。它只是显示'命令执行成功':( – 2014-10-08 06:48:58

+0

你需要在这里更新你的c#代码@nowhewhomustnotbenamed。 – Neel 2014-10-08 06:50:45

+0

我还没有这样做Neel。我将学习如何编写和更新帖子。谢谢:) – 2014-10-08 06:57:07

2

你可以这样做:

存储过程

CREATE PROCEDURE StoredProcedure1 
@oResult int output 
AS 
BEGIN 
    SET @oResult = 2 
END 

,然后调用它像这样:

DECLARE @Result INT 
exec StoredProcedure1 @oResult = @Result output 
SELECT @Result 

这将输出

2 

更新:

就像在评论处理。你也可以简化陈述。通过这样做:

DECLARE @Result INT 
exec StoredProcedure1 @Result output 
SELECT @Result 

参考:

+0

Is there需要'@ oResult = @结果输出'? 'exec StoredProcedure1 @ Result output; SELECT @ Result'是否也能工作? – artm 2014-10-08 07:19:21

+0

@artm:你说得对。我更新了答案 – Arion 2014-10-08 07:37:03

3

在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); 
} 

当然,在存储过程,你应该设置以某种方式在其他的答案中所解释的@oResult参数,但如果您使用的OUTPUT参数没有必要返回相同的值。

但是,如果需要,您可以同时具有OUTPUT参数和RETURN值。在这种情况下,您的来自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); 
} 

但请记住,RETURN values are limited to Integer Expressions

+0

非常感谢。现在去试试) – 2014-10-08 07:02:35

0
CREATE PROCEDURE SP1 
(
@oResult int output 
) 
AS 
BEGIN 
     SET @oResult=2 
     Select @oResult 
END