简单的数学存储过程返回NULL?

时间:2011-08-16 09:37:20

标签: sql-server sql-server-2005 tsql stored-procedures

如果有人能让我知道为什么这个存储过程返回NULL(我使用SQL Server 2005),我将不胜感激?谢谢你的帮助。

CREATE PROCEDURE calc_runningtotal_averageprice_realisedpl
    @filled_size                REAL,
    @reported_execution         REAL,
    @old_running_total          REAL,
    @old_average_price          REAL,
    @new_running_total          REAL OUTPUT,
    @new_average_price          REAL OUTPUT,
    @realised_pl                REAL OUTPUT
AS
BEGIN

    SET @new_running_total = @old_running_total + @filled_size 
    SET @realised_pl = 0
    IF SIGN(@filled_size) = SIGN(@old_running_total)
        BEGIN
            SET @new_average_price = (@filled_size * @reported_execution + @old_running_total * @old_average_price) / (@new_running_total)
        END
    ELSE
        BEGIN
            DECLARE @quantity           REAL
            IF ABS(@reported_execution) < ABS(@old_running_total)
                SET @quantity = ABS(@reported_execution)
            ELSE
                SET @quantity = ABS(@old_running_total);


            SET @realised_pl = (@reported_execution - @old_average_price) * @quantity * SIGN(@filled_size) * -1;
            SET @new_average_price = 
                CASE
                    WHEN ABS(@filled_size) < ABS(@old_running_total) THEN @old_average_price
                    WHEN ABS(@filled_size) = ABS(@old_running_total) THEN 0
                    WHEN ABS(@filled_size) > ABS(@old_running_total) THEN @reported_execution
                END
        END
END

如果我运行以下内容,我会得到3个NULLS

DECLARE @new_running_total      REAL
DECLARE @new_average_price      REAL
DECLARE @realised_pl            REAL

EXEC calc_runningtotal_averageprice_realisedpl 1, 1, 2, 2, @new_running_total, @new_average_price, @realised_pl
SELECT @new_running_total, @new_average_price, @realised_pl

我期待像3,1.666666,

这样的东西

1 个答案:

答案 0 :(得分:9)

您只是不使用OUTPUT

来修改通话中的输出参数
EXEC calc_runningtotal_averageprice_realisedpl 1, 1, 2, 2, 
   @new_running_total OUTPUT, 
   @new_average_price OUTPUT, 
   @realised_pl OUTPUT