我在Access中有以下代码,我正在迁移到SQL Server,
IIf([code] Is Not Null,IIf([code]<>'ABC',
IIf([length] Is Null,1,IIf([Length]=0,1,
IIf([width] Is Null,1,IIf([Width]=0,1,
IIf([height] Is Null,1,IIf([Height]=0,1,
0))))))))
我相信它会检查代码不为空且不等于'ABC'然后长度应为null或0然后它分配条件的值1失败然后它是0.我需要一点帮助用case写它
答案 0 :(得分:3)
IIf
转换为SQL Server中的CASE WHEN
,如下所示:
IIf(condition, whenTrue, whenFalse)
直接翻译为
CASE WHEN condition THEN whenTrue ELSE whenFalse END
对于嵌套表达式
IIf(condition1, whenTrue1, IIf(condition2, whenTrue2, whenFalse))
你可以直接翻译它们:
CASE WHEN condition1
THEN whenTrue1
ELSE CASE WHEN condition2
THEN whenTrue2
ELSE whenFalse
END
END
或使用CASE允许您指定多个WHEN
条件的事实:
CASE WHEN condition1 THEN whenTrue1
WHEN condition2 THEN whenTrue2
ELSE whenFalse
END
有了这些知识,翻译你的陈述应该很容易,并留给读者练习(我不想破坏乐趣)。
答案 1 :(得分:3)
直译:
CASE WHEN [code] Is Not Null
THEN
CASE WHEN [code]<>'ABC'
THEN
CASE WHEN [length] Is Null
THEN 1
ELSE
CASE WHEN [Length]=0
THEN 1
ELSE
CASE WHEN [width] Is Null
THEN 1
ELSE
CASE WHEN [Width]=0
THEN 1
ELSE
CASE WHEN [height] Is Null
THEN 1
ELSE
CASE WHEN [Height]=0
THEN 1
ELSE 0
END
END
END
END
END
END
END
END
简化:
CASE WHEN ISNULL([code], 'ABC') <>'ABC' AND
(ISNULL([length], 0) = 0 OR
ISNULL([width], 0) = 0 OR
ISNULL([height], 0) = 0)
THEN 1
ELSE 0
END