2016-11-30 129 views
0

問題:PassedPercent返回0,但應該有一個數字(我可以在所有情況下確認@Started和@Passed有數值)。相同的公式在.aspx頁面上正確工作,但我將其轉換爲SQL查詢。返回零不是值

代碼:

USE DATABASE 

DECLARE @i INT = 0 
DECLARE @ModuleID bigint 
DECLARE @Started bigint 
DECLARE @Completed bigint 
DECLARE @Passed bigint 

DECLARE merge_cursor CURSOR FAST_FORWARD FOR SELECT [ModuleID] FROM dbo.[TblModules] 
--ORDER BY [ModuleID] 
OPEN merge_cursor 

FETCH NEXT FROM merge_cursor INTO @ModuleID 

WHILE @@FETCH_STATUS = 0 
BEGIN 

    SET @Started = (Select Count(ID) FROM [TblResults] WHERE [ModuleID] = @ModuleID) 
    SET @Completed = (Select Count(ID) FROM [TblResults] WHERE [ModuleID] = @ModuleID and ModuleDatecomplete <> '') 
    SET @Passed = (Select Count(ID) FROM [TblResults] WHERE [ModuleID] = @ModuleID and Pass = 'Yes') 

    UPDATE [TblModules] SET [Started] = @Started,[Completed] = @Completed,[Passed] = @Passed,[PassedPercent] = ((@Passed/@Started) * 100) 
    WHERE [ModuleID] = @ModuleID  

    FETCH NEXT FROM merge_cursor INTO @ModuleID 
END 

CLOSE merge_cursor 
DEALLOCATE merge_cursor 
+0

零是一個數字。 – jarlh

+0

@jarlh,但只有足夠大的值爲0.:p – xQbert

回答

3

的int/INT將返回int(在該情況下爲零)

嘗試

,[PassedPercent] = (@Passed *100.0)/@Started 
+2

@indofraiser當'@Passed <@ Started'(假設兩者均爲正數)時,您問題中的公式始終返回0,因爲「如果整數除數被除以整數除數,結果是一個整數,其結果的任何小數部分被截斷。「 (請參閱https://msdn.microsoft.com/en-us/library/ms175009.aspx),所以John是對的! –

+0

乾杯,我剛剛刪除了我的其他評論,並且要問你的答案!) – indofraiser

+0

@indofraiser給它一個鏡頭,你可能會感到驚訝。例如,嘗試:選擇25/100,然後嘗試選擇25.0/100第一個將返回一個零,而第二個將返回0.25 –