SSRS 中的惰性评估
我正在使用 SSRS 2005 生成报告,报告中的一列是简单的均值计算。 我不想除以零,所以对于我输入的文本框值:
=Switch(Fields!Count.Value=0,0,Fields!Count.Value>0,Fields!Sum.Value/Fields !Count.Value)
这仍然计算第二个表达式。
也是如此:
=IIF(Fields!Count.Value=0,0,Fields!Sum.Value/Fields!Count.Value)
我不希望我的报告显示错误。 我怎样才能克服这个问题?
I'm using SSRS 2005 to produce a report, and one of the columns in my report is a simple mean calculation. I don't want to divide by zero, so for the textbox value I have put:
=Switch(Fields!Count.Value=0,0,Fields!Count.Value>0,Fields!Sum.Value/Fields!Count.Value)
This still evaluates the second expression.
And so does:
=IIF(Fields!Count.Value=0,0,Fields!Sum.Value/Fields!Count.Value)
I don't want my report to display errors. How can I overcome this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不幸的是,因为 IIF 实际上只是一个函数,所以所有参数在调用函数之前都会被计算(导致除以零)。
在表达式中嵌入复杂逻辑的一种方法是在报表中嵌入函数。 您可以编写一个 VB 函数来返回“报告属性”的“代码”选项卡中您喜欢的任何内容。
在文本框文本表达式属性中:
Unfortunately because IIF is actually just a function, all arguments get evaluated before the function gets called (resulting in your divide by zero).
One way of embedding complex logic in an expression is to embed a function in the report. You can write a VB function to return whatever you like in the Code Tab of the Report Properties.
In the Textbox Text expression property:
检查零时,我总是对表达式求反,如下所示:
这样做的另一个好处是,可以在
Count
字段为空/null 的情况下避免除法。When checking for zeros, I always negate the expression like this:
This has the added benefit of avoiding the division in case the
Count
field is empty/null.试试这个:
=IIf(Fields!Count.Value = 0, 0, Fields!Sum.Value / IIf(Fields!Count.Value = 0, 1, Fields!Count.Value))
Try this:
=IIf(Fields!Count.Value = 0, 0, Fields!Sum.Value / IIf(Fields!Count.Value = 0, 1, Fields!Count.Value))
这是一直对我有用的解决方案:
避免除以零错误。
如果报告有太多部门,就会变得乏味。 我更喜欢一些VB代码来进行除法并处理除零。 不确定过多的 vb 代码是否会降低报告的性能。
这是 如何用 vb 做到这一点
将 he 链接替换为 archive.org 链接。 以下是该链接的相关内容:
Here is solution that has always worked for me:
Avoiding Divide By Zero Errors .
It gets tedious if the report has too many divisions. I prefer some vb code to do the division and handle division by zero. Not sure if too much of vb code would bring down the performance of the report.
Here is how to do it with vb
Replaced he link with archive.org link. Here is the relevant content from that link: