SQL Server中存储过程的输出参数

时间:2021-03-24 11:24:31

In SQL Server, is an OUTPUT parameter actually an INPUT/OUTPUT parameter? I'm assuming this since even if we specify the OUTPUT keyword for a parameter in the stored procedure definition, we are still required to supply value for this parameter when calling a stored procedure.

在SQL Server中,OUTPUT参数实际上是INPUT / OUTPUT参数吗?我假设这是因为即使我们在存储过程定义中为参数指定了OUTPUT关键字,我们仍然需要在调用存储过程时为此参数提供值。

3 个解决方案

#1


2  

Yes, you have to provide a value for the output parameter.

是的,您必须为输出参数提供值。

For example, of you create a stored procedure like this:

例如,您创建一个这样的存储过程:

CREATE PROC sales_for_type @type VARCHAR(55), @total_sales INT OUTPUT
AS
SELECT SUM(qty) FROM sales a, titles b
WHERE
a.title_id = b.title_id
and
b.type = @type

and then you call it like:

然后你称之为:

DECLARE @total_sales_business int
EXEC sales_for_type business, @total_sales=@total_sales_business OUTPUT

Check this article.

看看这篇文章。

#2


1  

Yes, an output parameter is also an input parameter.

是的,输出参数也是输入参数。

#3


0  

Yes it is both input and output as others already answered. But in case you don't want to be forced to give an input parameter, you can easily set a default value in the parameter declaration.
See also this question: Can I have an optional OUTPUT parameter in a stored procedure?

是的,它是输入和输出,因为其他人已经回答。但是如果您不想强制提供输入参数,可以在参数声明中轻松设置默认值。另请参阅此问题:我可以在存储过程中使用可选的OUTPUT参数吗?

#1


2  

Yes, you have to provide a value for the output parameter.

是的,您必须为输出参数提供值。

For example, of you create a stored procedure like this:

例如,您创建一个这样的存储过程:

CREATE PROC sales_for_type @type VARCHAR(55), @total_sales INT OUTPUT
AS
SELECT SUM(qty) FROM sales a, titles b
WHERE
a.title_id = b.title_id
and
b.type = @type

and then you call it like:

然后你称之为:

DECLARE @total_sales_business int
EXEC sales_for_type business, @total_sales=@total_sales_business OUTPUT

Check this article.

看看这篇文章。

#2


1  

Yes, an output parameter is also an input parameter.

是的,输出参数也是输入参数。

#3


0  

Yes it is both input and output as others already answered. But in case you don't want to be forced to give an input parameter, you can easily set a default value in the parameter declaration.
See also this question: Can I have an optional OUTPUT parameter in a stored procedure?

是的,它是输入和输出,因为其他人已经回答。但是如果您不想强制提供输入参数,可以在参数声明中轻松设置默认值。另请参阅此问题:我可以在存储过程中使用可选的OUTPUT参数吗?