SQLServerIIF 与 CASE

最近,我了解了 SQLServer2012中 IIF函数的可用性。我总是在查询中使用嵌套的 CASE。我想知道 IIF语句的确切用途,以及在查询中什么时候应该使用 IIF而不是 CASE语句。 我在查询中大多使用嵌套的 CASE

谢谢你的帮忙。

138734 次浏览

IIF is the same as CASE WHEN <Condition> THEN <true part> ELSE <false part> END. The query plan will be the same. It is, perhaps, "syntactical sugar" as initially implemented.

CASE is portable across all SQL platforms whereas IIF is SQL SERVER 2012+ specific.

IIF is a non-standard T-SQL function. It was added to SQL SERVER 2012, so that Access could migrate to SQL Server without refactoring the IIF's to CASE before hand. Once the Access db is fully migrated into SQL Server, you can refactor.