2011-08-16 3 views
4

J'apprécierais que quelqu'un puisse me dire pourquoi cette procédure stockée renvoie NULL (j'utilise SQL Server 2005)? Merci pour l'aide.Procédure mathématique simple stockée Retourne NULL?

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 

Si je cours ce qui suit, je reçois 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 

J'attends quelque chose comme 3, 1,66666, 0

+1

par la solution d'Alex K. En fait, dans la plupart des langues avec lesquelles j'ai travaillé, vous devez spécifier explicitement qu'une variable est transmise par référence avec un ou plusieurs mots-clés dans l'instruction d'appel. – deutschZuid

Répondre

9

Vous êtes tout simplement pas décorer les paramètres de sortie dans l'appel avec OUTPUT:

EXEC calc_runningtotal_averageprice_realisedpl 1, 1, 2, 2, 
    @new_running_total OUTPUT, 
    @new_average_price OUTPUT, 
    @realised_pl OUTPUT 
+0

J'ai oublié de mentionner, merci pour l'aide ... – Bertie

Questions connexes