问题
I couldn't get this expression working with reporting services. I have to use IF and ELSE IF statement. Thank you for any suggestions.
= Switch( IsNothing(Fields!field_date.Value),"", Fields!set_flag.Value=1,"Declined", Fields!field_name.Value)
Here is what I am trying to do.
If(IsNothing(Fields!field_date.Value)) Then "";
ElseIf Fields!set_flag.Value=1 Then "Declined";
Else Fields!field_name.Value
回答1:
Doesn't SSRS use the VB Runtime Library? In which case, the switch statement is documented here and states that you must have an even number of elements passed in. If this is your issue, you're probably getting an ArgumentException
thrown.
If you want to have a default return value, you'd have to add a dummy condition that always evaluates to true:
= Switch( IsNothing(Fields!field_date.Value),"", Fields!set_flag.Value=1,"Declined", True, Fields!field_name.Value)
回答2:
I would use a code block like gbn suggested. It will be easier to read. Here is a how to from Microsoft about used code blocks in Reporting Services: http://msdn.microsoft.com/en-us/library/ms156028%28v=sql.100%29.aspx
If you don't want to use a code block then you can chain IIF statements in your expression. Looking at your example it would be something like
=IIF(IsNothing(Fields!field_date.Value),"",IIF(Fields!set_flag.Value=1,"Declined",Fields!field_name.Value)
Any more than 2 IIF or anything more complex than just return a value, then I would definitely go for a code block. Otherwise you are just making it that much worse for the person who has to maintain the report after you.
来源:https://stackoverflow.com/questions/7996571/reporting-services-expression-using-switch