問題: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
零是一個數字。 – jarlh
@jarlh,但只有足夠大的值爲0.:p – xQbert