嵌套的 IIf 语句有效,但 Switch 语句在 MS Access 中产生 'Overflow' 错误。为什么?
Nested IIf statements work but Switch statements produce an 'Overflow' error in MS Access. Why?
我正在处理的查询显示保单成本占个人总收入的百分比。
嵌套 IIf 方法:
percentage: IIf([general_income]=0,"No income",
IIf(IsNull([PolicyValue]),"No policy",
IIf([PolicyValue]>[general_income],"Error",
Format([PolicyValue]/[general_income],"Percent"))))
Switch 方法:
percentage: Switch(
[general_income]=0,"No income",
IsNull([PolicyValue]),"No policy",
[PolicyValue]>[general_income],"Error",
True,Format([PolicyValue]/[general_income],"Percent")
)
我最初的方法是 Switch 方法,在调试时我假设溢出错误是由除以零引起的,因为关于这个主题的其他各种帖子都指向那里,在我的例子中,当除以一个随机数时(例如 1 ) general_income
除以零代替了溢出错误。
但是,我不明白 general_income
怎么会是 0,因为第一个案例解决了这个问题。关于在 Switch 场景中导致溢出错误的原因有什么想法吗?
Switch evaluates all of the expressions, even though it returns
only one of them. For this reason, you should watch for undesirable
side effects. For example, if the evaluation of any expression results
in a division by zero error, an error occurs.
所以第一种情况不考虑 general_income
可能为 0 的事实,然后 [PolicyValue]/[general_income]
抛出错误。
我正在处理的查询显示保单成本占个人总收入的百分比。
嵌套 IIf 方法:
percentage: IIf([general_income]=0,"No income",
IIf(IsNull([PolicyValue]),"No policy",
IIf([PolicyValue]>[general_income],"Error",
Format([PolicyValue]/[general_income],"Percent"))))
Switch 方法:
percentage: Switch(
[general_income]=0,"No income",
IsNull([PolicyValue]),"No policy",
[PolicyValue]>[general_income],"Error",
True,Format([PolicyValue]/[general_income],"Percent")
)
我最初的方法是 Switch 方法,在调试时我假设溢出错误是由除以零引起的,因为关于这个主题的其他各种帖子都指向那里,在我的例子中,当除以一个随机数时(例如 1 ) general_income
除以零代替了溢出错误。
但是,我不明白 general_income
怎么会是 0,因为第一个案例解决了这个问题。关于在 Switch 场景中导致溢出错误的原因有什么想法吗?
Switch evaluates all of the expressions, even though it returns only one of them. For this reason, you should watch for undesirable side effects. For example, if the evaluation of any expression results in a division by zero error, an error occurs.
所以第一种情况不考虑 general_income
可能为 0 的事实,然后 [PolicyValue]/[general_income]
抛出错误。