SSRS中LookupSet搭配Join正常、调用Count返回#Error问题咨询
报错原因
这个问题由SSRS表达式的两个核心特性共同导致:
- SSRS的
IIf函数不支持短路求值:无论判断条件的结果是真还是假,IIf的真分支、假分支都会被完整计算,不会因为条件满足就跳过另一个分支的运算。 - SSRS内置
Count函数的适用场景不包含数组计数:Count是数据集聚合函数,设计用来统计数据集范围内的字段值,不能直接对LookupSet返回的内存数组执行计数操作,获取LookupSet返回结果长度的正确写法是调用返回数组的Length属性。
两个表达式的差异说明
- 可正常运行的第一个表达式:
当Fields!EntityID.Value Is Nothing为真时,假分支的Join(LookupSet(...), ",")依然会被计算,但LookupSet传入空参数返回空数组时,Join对空数组运算只会返回空字符串,不会触发运行时错误。
=IIf( Fields!EntityID.Value Is Nothing, Fields!Foo.Value, Join( LookupSet( Fields!EntityID.Value, Fields!EntityID.Value, Fields!Bar.Value, "dsMain" ) , ",") )
- 报错的第二个表达式:
无论Fields!EntityID.Value是否为空,Count(LookupSet(...))这部分代码都会被执行,而Count本身不支持接收数组作为入参,直接触发运算错误,最终整个表达式返回#Error。
=IIf( Fields!EntityID.Value Is Nothing, Fields!Foo.Value, Count( LookupSet( Fields!EntityID.Value, Fields!EntityID.Value, Fields!Bar.Value, "dsMain" ) ) )
修复方案
将第二个表达式中的Count()替换为数组的Length属性即可正常运行,修正后代码如下:
=IIf( Fields!EntityID.Value Is Nothing, Fields!Foo.Value, LookupSet( Fields!EntityID.Value, Fields!EntityID.Value, Fields!Bar.Value, "dsMain" ).Length )
内容的提问来源于stack exchange,提问作者MMalke
相关产品推荐
相关产品推荐

