0

As a part of getting to learn Stored procedures , I came up with this.

CREATE PROCEDURE StoredProcedure1
@oResult int output
AS 
BEGIN
  SELECT @oResult = 2
  RETURN @oResult
END
  1. But the problem is on execution of this SP, it just returns something like. 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 ?

  1. I supplied as it was asking,EXEC StoredProcedure1 @oResult = 0 but it just says Command Completed Successfully but no output.

Any ideas why ?

5
  • Could you show the C# code that calls this procedure? The problem is there. Commented Oct 8, 2014 at 6:49
  • @Steve: I didnot write any C# code Steve. I just wrote this to learn Stored Procedures. This is just a mssql sp right? C# just calls this right? Commented Oct 8, 2014 at 6:51
  • Try Select @ oResult instead of return @ oResult Commented Oct 8, 2014 at 6:51
  • @Steve: Yup, i am interested on that too. I have used Dapper before. But yet to learn how to call this SP from it. Can you guide me with any skeleton please? I will build upon that Commented Oct 8, 2014 at 6:54
  • @Steve: I assume this Sp expects a out parameter like the out keyword in C#. Am I correct ? Commented Oct 8, 2014 at 6:56

4 Answers 4

3

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.

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.

However you could have both an OUTPUT parameter and a RETURN value if you need to. In this case your call from C# should be

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. Going to try now )
2

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:

1 Comment

Is there a need for '@ oResult = @ Result output'? Wouldn't 'exec StoredProcedure1 @ Result output;SELECT @ Result' work as well?
0

You do not need to write return ..try below code :-

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

3 Comments

Thanks. But again, it expects me to compulsorily give a parameter and also it displays no result . It just shows Command executed successfully :(
you need to update your c# code here @nowhewhomustnotbenamed.
I am yet to do that Neel. I will learn how to write that and update the post. Thank you :)
0
CREATE PROCEDURE SP1
(
@oResult int output
)
AS 
BEGIN
       SET @oResult=2
       Select @oResult
END

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.