Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use OUTPUT parameter in Stored Procedure

I am new to writing Stored Procedure. So I wrote one with output parameters and want to access the output value, hot to do it.

My Stored Procedure:

ALTER PROCEDURE selQuery
    (
        @id int, @code varchar(50) OUTPUT
    )
AS
    SELECT RecItemCode = @code, RecUsername from Receipt where RecTransaction = @id
    RETURN @code

If trying to set "@code=RecItemCode" getting error as : "A SELECT statement that assigns a value to a variable must not be combined with Data Retrieval operations."

And I am using the Stored Procedure as:

con.Open();
cmd.Parameters.AddWithValue("@id", textBox1.Text);
SqlParameter code = new SqlParameter("@code", SqlDbType.Int);
code.Direction = ParameterDirection.Output;
cmd.Parameters.Add(code);
SqlDataReader sdr = cmd.ExecuteReader();
MessageBox.Show(cmd.Parameters["@code"].Value.ToString()); // getting error
con.Close();

Error : "Object reference not set to an instance of an object." I want to get the value of output parameter. How to get that?

Thanks.

like image 486
Sandy Avatar asked Oct 14 '11 16:10

Sandy


People also ask

How to use output parameters in a stored procedure to return multiple outputs?

Let us see how to use Output Parameters in a Stored procedure to return multiple outputs. Here, we declare a variable called @LastName as input parameter, and three Output parameter. Within the procedure, we are finding the Occupation, Education, and yearly Income of an employee whose last name is equal to the input parameter.

How do you call a stored procedure in SQL with parameters?

Code language:SQL (Structured Query Language)(sql) Calling stored procedures with output parameters To call a stored procedure with output parameters, you follow these steps: First, declare variablesto hold the values returned by the output parameters Second, use these variables in the stored procedure call.

How to use variables in stored procedures?

First, declare variablesto hold the values returned by the output parameters Second, use these variables in the stored procedure call. For example, the following statement executes the uspFindProductByModelstored procedure:

What does it mean to have an output parameter?

Having an output parameter means you want to return some value from the stored procedure and use it in ADF. In this example, the return value will control the next activities in the pipeline. Let's store the return value into a variable.


1 Answers

There are a several things you need to address to get it working

  1. The name is wrong its not @ouput its @code
  2. You need to set the parameter direction to Output.
  3. Don't use AddWithValue since its not supposed to have a value just you Add.
  4. Use ExecuteNonQuery if you're not returning rows

Try

SqlParameter output = new SqlParameter("@code", SqlDbType.Int);
output.Direction = ParameterDirection.Output;
cmd.Parameters.Add(output);
cmd.ExecuteNonQuery();
MessageBox.Show(output.Value.ToString());
like image 140
Conrad Frix Avatar answered Dec 01 '22 00:12

Conrad Frix