我在SSRS中使用了如图所示的switch语句。
=switch(Fields!TOTAL_TIME_HRS.Value=0, 0,
Fields!TOTAL_TIME_HRS.Value<0, -1,
Fields!TOTAL_TIME_HRS.Value>0, 1/Fields!TOTAL_TIME_HRS.Value)
执行我的报表会抛出一个除以0的警告,对于Fields!TOTAL_TIME_HRS.Value确实等于0的行,报表上的结果是 "#Error"。 switch语句不是应该捕捉到可能的错误,并避免它吗?
不,SSRS会评估两边的情况
解决这个问题的一个方法是在你的报表中加入一些VB代码来处理可能出现的除以零的错误。
右键点击您的报表,选择报表属性--进入代码选项卡,输入以下代码。
Public Function NDZ(Byval a As Decimal,Byval b As Decimal, Byval c As Decimal) As Decimal
' Fix for divide by zero problem in VB
' calculates a/b and either returns result or c if b = 0
if b = 0 then
return c
else
return a/b
end if
end function
现在,在你的报告中,你要对细胞进行分裂。
输入以下内容。
=code.NDZ(1,Fields!TOTAL_TIME_HRS.Value,0)
或者你可以简单地用=code.ndz位来替换你的开关语句中的除法部分。