返回零而不是值

时间:2016-11-30 13:37:08

标签: sql sql-server

问题: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 

1 个答案:

答案 0 :(得分:3)

int / int将返回一个int(在本例中为零)

尝试

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