IIF(…) not a recognized built-in function

丶灬走出姿态 提交于 2019-12-06 18:19:55

问题


I'm trying to use this in Microsoft SQL Server 2008 R2:

SET @SomeVar = @SomeOtherVar +
  IIF(@SomeBool, 'value when true', 'value when false')

But I get an error:

IIF(...) is not a recognized built-in function name

Is IIF() only compatible with a later version?

Is there an alternate function I can use?


回答1:


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



回答2:


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 (Transact SQL)



回答3:


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).




回答4:


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


来源:https://stackoverflow.com/questions/12035062/iif-not-a-recognized-built-in-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!