2014-11-24 33 views
0

我已經創建了下面的函數,並且它似乎隨機將@FCST和@QTY值更改爲隨機選擇。下面是調試中返回錯誤值爲@FCST的程序示例。由於@FCST在9.53,我預計它會返回9.53。 enter image description hereSQL函數 - 將變量設置爲隨機值

CREATE FUNCTION dbo.LTBADJ 
(@MAT VARCHAR(30),@LCUT DATE, @QS DATE,@STAT FLOAT, @MCA FLOAT) 
RETURNS FLOAT 
AS BEGIN 
DECLARE @HD DATE 
DECLARE @ED DATE 
DECLARE @FCST FLOAT 
DECLARE @QTY FLOAT 
DECLARE @YRS FLOAT 

SET @ED = 
(SELECT TOP 1 [EO_END_DATE] FROM [dbo].[EO_LTB] 
WHERE [W_PART_NUMBER] = @MAT 
AND [APPROVED_DATE] <= @LCUT 
ORDER BY [EO_END_DATE] DESC) 
SET @HD =DATEADD(YEAR,2,@QS) 
SET @FCST = (CASE WHEN @MCA <= @STAT THEN @MCA ELSE @STAT END) 
SET @FCST = (CASE WHEN @FCST <.5 THEN .5 ELSE @FCST END) 
SET @YRS = (DATEDIFF(DAY,@HD,@ED) + 730)/365 
SET @QTY = @FCST * @YRS 
SET @QTY = (CASE WHEN @ED<[email protected] THEN 0 ELSE @QTY END) 
RETURN @QTY 
END ; 

編輯:@QTY的實例計算錯誤 enter image description here

+1

嗯...這不是'9.53' ..這'9.53×10^-2' ='9.53 X 0.01' ..這是0.0953 – 2014-11-24 21:52:09

+0

並且結果是'5×10^-1' ='5 x 0.1' ='0.5' ..這就是你指定它的時候少於0.5 – 2014-11-24 21:53:50

+0

在你的更新中,QTY是'21.345 +'..應該是什麼? – 2014-11-24 21:58:41

回答

1

所有似乎是正確的 - 你可以看到發生了什麼事情通過運行下面的SQL。 我懷疑數字結尾處的混淆是e-002e-001;這只是SQL試圖顯示浮動值;由於浮點運算是如何工作的,一些數字會從你期望的中稍微計算出來,然後爲了使它們可讀,SQL將顯示帶有指數的數字。

declare @MAT VARCHAR(30) = '50309120000W' 
,@LCUT DATE = '2014-10-26' 
, @QS DATE = '2014-10-27' 
,@STAT FLOAT = 9.5399999999999999e-002 
, @MCA FLOAT = 9.5399999999999999e-002 


DECLARE @HD DATE 
DECLARE @ED DATE 
DECLARE @FCST FLOAT 
DECLARE @QTY FLOAT 
DECLARE @YRS FLOAT 

SET @ED = 
(
    SELECT TOP 1 [EO_END_DATE] 
    FROM (SELECT '2019-02-28' [EO_END_DATE], @MAT [W_PART_NUMBER], @LCUT [APPROVED_DATE]) X 
    WHERE [W_PART_NUMBER] = @MAT 
    AND [APPROVED_DATE] <= @LCUT 
    ORDER BY [EO_END_DATE] DESC 
) 
SET @HD =DATEADD(YEAR,2,@QS) 


SET @FCST = (CASE WHEN @MCA <= @STAT THEN @MCA ELSE @STAT END) 
SELECT @FCST 

SET @FCST = (CASE WHEN @FCST <.5 THEN .5 ELSE @FCST END) 
SELECT @FCST 

SET @YRS = (DATEDIFF(DAY,@HD,@ED) + 730)/365 
SET @QTY = @FCST * @YRS 
SELECT @FCST, @YRS, @QTY 

SET @QTY = (CASE WHEN @ED<[email protected] THEN 0 ELSE @QTY END) 
SELECT @QTY, @ED, @HD 
+0

所以是的,我錯誤地閱讀了花車的價值。謝謝。 – 2014-11-24 22:35:31