IIF(…) not a recognized built-in function

南楼画角 提交于 2019-12-04 22:20:48
Richard

As others have said, IIF comes from SQL 2012. Before then, you can use CASE:

SET @SomeVar = @SomeOtherVar + CASE
 WHEN @SomeBool
 THEN 'value when true'
 ELSE 'value when false'
END

What's New in SQL Server 2012, Programmability Enhancements:

SQL Server 2012 introduces 14 new built-in functions. These functions ease the path of migration for information workers by emulating functionality that is found in the expression languages of many desktop applications. However these functions will also be useful to experienced users of SQL Server.

...

IIF is not valid for SQL Server 2008 R2 and any version before that.

IIF was introduced in SQL Server 2012 (there is no link to previous versions on the documentation page I have linked to).

You could also use the standard IF statement if it's outside a select.

E.g.

DECLARE @Answer VARCHAR(3) = 'YES'

IF @Answer = 'Yes'
BEGIN 
--Do Something if true
END
ELSE
-- Do Soemthing if false
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!