SQL Server 2012 使用存储过程将存储过程的 Return 值写入单元格

SQL Server 2012 Write Return Values of Stored Procedure to Cells using Stored Procedure

我在 SQL Server 2012 中编写了一个存储过程,它接受三个输入值和 returns 在进行一些计算后得到一个结果(总和)。

DECLARE @return_value int,
        @total numeric(20, 2)

EXEC @return_value = [dbo].[storedprocedure]
                        @input1 = 'input_value',
                        @input2= 12345,
                        @input3 = 5

SELECT @total as '@total'

SELECT 'Return Value' = @return_value

这个returns一个值,例如

@total 1000,02  --this is the correct value which I want to use--

Return Value 1000

我有一个 table,其中一些列中的值我想用作输入,还有一个空列,我想在其中填充存储过程的 @total 变量的结果。

如果我没记错的话,那么需要第二个存储过程来执行第一个循环遍历所有行,并将值写入其中。

最好的方法是什么?

我不知道存储过程中有什么,但您可能可以创建一个函数。像这样:

CREATE FUNCTION [dbo].[YourFunctionName]
(
     @input1 VARCHAR(10)
    ,@input2 INT
    ,@input3 INT
)
RETURNS INT
AS
BEGIN
    -- Your code here.

    RETURN -- You return value here.
END

并像这样使用它:

UPDATE YourTable
SET YourColumn = dbo.YourFunctionName(Input1, Input2, Input3)

否则您必须声明一个游标,执行存储过程并对每一行执行更新。出于多种原因不推荐使用迭代方法,但我想这超出了这个问题的范围。