# SSRS_Switch_踩雷
###### tags: `SSRS` `Switch` `Reporting Service`
今天處理SSRS報表的時候一直撞牆,一個很簡單的`Switch`判斷函式也無法成功處理,欄位應該是`nothing`,但`Switch`判斷到最後就是會出現`#錯誤`,這種異常訊息通常出現在『除零』的情況,但我一直覺得第一個`Switch`條件就應該已經將欄位判斷為`nothing`,但似乎不是我想的那樣。
範例資料集如下:
```sql
select 1 a, 1 b from dual
union all
select 2, null from dual
union all
select null, 3 from dual
```
報表很簡單,如下圖:

```
=Switch(
IsNothing(Fields!A.Value), nothing,
IsNothing(Fields!B.Value), nothing,
True, (Fields!A.Value - Fields!B.Value) / Fields!A.Value
)
```
可以看的到條件判斷就是如果A、B任一欄位為`nothing`,則該欄為`nothing`,否則就`(A-B)/A`
但實際執行的時候卻發現,C欄出現`#錯誤`的訊息,如下圖:

這很怪,詢問老司機之後得知,用`Switch`,最後還是要預防一下除零的狀況,因此調整如下:
```
=Switch(
IsNothing(Fields!A.Value), nothing,
IsNothing(Fields!B.Value), nothing,
True, (Fields!A.Value - Fields!B.Value) / IIF(Fields!A.Value is nothing, 1, Fields!A.Value)
)
```
最後我加了一`IIF`來判斷,如果是`nothing`就賦值1,再來一次,神奇的是,就正常了,如下圖:

好吧,也許`SSRS`的`Switch`不一樣。