存储过程中输出参数的使用/好处

时间:2022-02-13 02:16:32

Where do we need to use output parameter in a stored procedure?

我们在存储过程中需要使用输出参数?

Does anyone have any real time experience to share?

有没有人有实时经验可以分享?

I searched a lot but not able to understand proper use of output parameter.

我搜索了很多,但无法理解输出参数的正确使用。

3 个解决方案

#1


There are several good answers already, but one not mentioned is when you need the procedure to return more than one variable.

已经有几个好的答案,但没有提到的是当你需要程序返回多个变量时。

Example: Searching for the highest salary a procedure might also return the I.D. of the person and whether they are still employed.

示例:搜索最高工资,程序也可能返回I.D.该人以及他们是否仍然受雇。

#2


There are many occasions where you want to get some data back from a stored procedure in the form of an output parameter:

在很多情况下,您希望以输出参数的形式从存储过程中获取一些数据:

  • when inserting data to a table and you need to get the identity value back
  • 将数据插入表时,需要返回标识值

  • when performing select statements and you need some extra data
  • 执行select语句时,您需要一些额外的数据

  • when updating or inserting data and you need some way to know if the operation was successful
  • 更新或插入数据时,您需要一些方法来了解操作是否成功

  • for most if not all of the reasons you need out or ref parameters in c#
  • 对于大多数(如果不是所有)你需要的原因或参考c#中的参数

There are probably more situations where an output is useful, but I think that should answer your question.

可能有更多情况下输出是有用的,但我认为这应该回答你的问题。

#3


It is to pass a value out of the stored procedure. Either a rowset reference, or a scalar value. Think of it as turning stored procedures into function calls.

它是从存储过程中传递一个值。行集引用或标量值。可以将其视为将存储过程转换为函数调用。

#1


There are several good answers already, but one not mentioned is when you need the procedure to return more than one variable.

已经有几个好的答案,但没有提到的是当你需要程序返回多个变量时。

Example: Searching for the highest salary a procedure might also return the I.D. of the person and whether they are still employed.

示例:搜索最高工资,程序也可能返回I.D.该人以及他们是否仍然受雇。

#2


There are many occasions where you want to get some data back from a stored procedure in the form of an output parameter:

在很多情况下,您希望以输出参数的形式从存储过程中获取一些数据:

  • when inserting data to a table and you need to get the identity value back
  • 将数据插入表时,需要返回标识值

  • when performing select statements and you need some extra data
  • 执行select语句时,您需要一些额外的数据

  • when updating or inserting data and you need some way to know if the operation was successful
  • 更新或插入数据时,您需要一些方法来了解操作是否成功

  • for most if not all of the reasons you need out or ref parameters in c#
  • 对于大多数(如果不是所有)你需要的原因或参考c#中的参数

There are probably more situations where an output is useful, but I think that should answer your question.

可能有更多情况下输出是有用的,但我认为这应该回答你的问题。

#3


It is to pass a value out of the stored procedure. Either a rowset reference, or a scalar value. Think of it as turning stored procedures into function calls.

它是从存储过程中传递一个值。行集引用或标量值。可以将其视为将存储过程转换为函数调用。