简体   繁体   English

如何在存储过程中使用OUTPUT参数

[英]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." 如果尝试将“@ code = RecItemCode”设置为获取错误:“为变量赋值的SELECT语句不得与Data Retrieval操作结合使用。”

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. 谢谢。

There are a several things you need to address to get it working 要使其正常工作,您需要解决几个问题

  1. The name is wrong its not @ouput its @code 这个名字是错的,而不是@ouput它的@code
  2. You need to set the parameter direction to Output. 您需要将参数方向设置为Output。
  3. Don't use AddWithValue since its not supposed to have a value just you Add . 不要使用AddWithValue因为它不应该只有你的Add值。
  4. Use ExecuteNonQuery if you're not returning rows 如果您没有返回行,请使用ExecuteNonQuery

Try 尝试

SqlParameter output = new SqlParameter("@code", SqlDbType.Int);
output.Direction = ParameterDirection.Output;
cmd.Parameters.Add(output);
cmd.ExecuteNonQuery();
MessageBox.Show(output.Value.ToString());

The SQL in your SP is wrong. SP中的SQL是错误的。 You probably want 你可能想要

Select @code = RecItemCode from Receipt where RecTransaction = @id

In your statement, you are not setting @code, you are trying to use it for the value of RecItemCode. 在您的语句中,您没有设置@code,您正在尝试将其用于RecItemCode的值。 This would explain your NullReferenceException when you try to use the output parameter, because a value is never assigned to it and you're getting a default null. 当您尝试使用输出参数时,这将解释您的NullReferenceException ,因为永远不会为其分配值,并且您将获得默认的null。

The other issue is that your SQL statement if rewritten as 另一个问题是你的SQL语句是否被重写为

Select @code = RecItemCode, RecUsername from Receipt where RecTransaction = @id

It is mixing variable assignment and data retrieval. 它混合了变量赋值和数据检索。 This highlights a couple of points. 这突出了几点。 If you need the data that is driving @code in addition to other parts of the data, forget the output parameter and just select the data. 如果除了数据的其他部分之外还需要驱动@code的数据,请忘记输出参数并只选择数据。

Select RecItemCode, RecUsername from Receipt where RecTransaction = @id

If you just need the code, use the first SQL statement I showed you. 如果您只需要代码,请使用我向您展示的第一个SQL语句。 On the offhand chance you actually need the output and the data, use two different statements 关于实际需要输出数据的随机机会,请使用两个不同的语句

Select @code = RecItemCode from Receipt where RecTransaction = @id
Select RecItemCode, RecUsername from Receipt where RecTransaction = @id

This should assign your value to the output parameter as well as return two columns of data in a row. 这应该将您的值赋给输出参数以及连续返回两列数据。 However, this strikes me as terribly redundant. 然而,这让我觉得非常多余。

If you write your SP as I have shown at the very top, simply invoke cmd.ExecuteNonQuery(); 如果您按照我在顶部显示的那样编写SP,只需调用cmd.ExecuteNonQuery(); and then read the output parameter value. 然后读取输出参数值。


Another issue with your SP and code. 您的SP和代码的另一个问题。 In your SP, you have declared @code as varchar . 在SP中,您已将@code声明为varchar In your code, you specify the parameter type as Int . 在代码中,将参数类型指定为Int Either change your SP or your code to make the types consistent. 更改SP或代码以使类型保持一致。


Also note: If all you are doing is returning a single value, there's another way to do it that does not involve output parameters at all. 另请注意:如果您所做的只是返回一个值,那么还有另一种方法可以完全不涉及输出参数。 You could write 你可以写

 Select RecItemCode from Receipt where RecTransaction = @id

And then use object obj = cmd.ExecuteScalar(); 然后使用object obj = cmd.ExecuteScalar(); to get the result, no need for an output parameter in the SP or in your code. 要获得结果,不需要SP或代码中的输出参数。

You need to define the output parameter as an output parameter in the code with the ParameterDirection.Output enumeration. 您需要使用ParameterDirection.Output枚举将输出参数定义为代码中的输出参数。 There are numerous examples of this out there, but here's one on MSDN . 有很多这方面的例子,但这是MSDN上的一个。

SqlCommand yourCommand = new SqlCommand();
yourCommand.Connection = yourSqlConn;
yourCommand.Parameters.Add("@yourParam");
yourCommand.Parameters["@yourParam"].Direction = ParameterDirection.Output;

// execute your query successfully

int yourResult = yourCommand.Parameters["@yourParam"].Value;

You need to close the connection before you can use the output parameters. 您需要先关闭连接,然后才能使用输出参数。 Something like this 像这样的东西

con.Close();
MessageBox.Show(cmd.Parameters["@code"].Value.ToString());

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

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