在WHERE子句中将IIf替换为SWITCH

时间:2013-09-07 19:23:52

标签: ms-access switch-statement ms-access-2003

我在MS-Access 2003中有以下查询,它可以正常工作:

SELECT tblDiscounts.DiscountID, tblDiscounts.DiscountPercent, tblDiscounts.DiscountName, tblDiscounts.DiscountDescription
FROM tblDiscounts, qryPropertyPeriodRate_Count_Nested
WHERE (tblDiscounts.DiscountID) = IIf ([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=1,1,IIf([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=2,2,IIf([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=3,3,4)));

我希望用Switch功能替换IIf功能,但无论我尝试什么都行不通。我最好的方法如下:

SELECT tblDiscounts.DiscountID, tblDiscounts.DiscountPercent, tblDiscounts.DiscountName, tblDiscounts.DiscountDescription
FROM tblDiscounts, qryPropertyPeriodRate_Count_Nested
WHERE (((tblDiscounts.DiscountID)=SWITCH ([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=1,1, [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=2,2, [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=3,3, [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]>3,4)));

但我收到了一条消息

  

表达式中的类型不匹配

请指教!

1 个答案:

答案 0 :(得分:2)

我可以看到的一个区别是,如果[qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]<1嵌套IIf将返回4,而Switch语句将返回Null。检查您的基础数据,看看是否会发生这种情况; Null值可能会搞乱WHERE子句。