IIF(...)不是公认的内置功能

时间:2022-12-15 09:34:48

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

我试图在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

IIF(...)不是公认的内置函数名称

Is IIF() only compatible with a later version?

IIF()仅与更高版本兼容吗?

Is there an alternate function I can use?

我可以使用其他功能吗?

4 个解决方案

#1


41  

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

正如其他人所说,IIF来自SQL 2012.在此之前,您可以使用CASE:

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

#2


8  

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

SQL Server 2012中的新增功能,可编程性增强功能:

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.

SQL Server 2012引入了14个新的内置函数。这些功能通过模拟许多桌面应用程序的表达式语言中的功能,简化了信息工作者的迁移路径。但是,这些功能对于有经验的SQL Server用户也很有用。

...

...

#3


4  

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

IIF对SQL Server 2008 R2及之前的任何版本均无效。

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

IIF是在SQL Server 2012中引入的(在我链接到的文档页面上没有链接到以前的版本)。

#4


4  

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

如果它在select之外,您也可以使用标准IF语句。

E.g.

例如。

DECLARE @Answer VARCHAR(3) = 'YES'

IF @Answer = 'Yes'
BEGIN 
--Do Something if true
END
ELSE
-- Do Soemthing if false

#1


41  

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

正如其他人所说,IIF来自SQL 2012.在此之前,您可以使用CASE:

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

#2


8  

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

SQL Server 2012中的新增功能,可编程性增强功能:

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.

SQL Server 2012引入了14个新的内置函数。这些功能通过模拟许多桌面应用程序的表达式语言中的功能,简化了信息工作者的迁移路径。但是,这些功能对于有经验的SQL Server用户也很有用。

...

...

#3


4  

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

IIF对SQL Server 2008 R2及之前的任何版本均无效。

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

IIF是在SQL Server 2012中引入的(在我链接到的文档页面上没有链接到以前的版本)。

#4


4  

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

如果它在select之外,您也可以使用标准IF语句。

E.g.

例如。

DECLARE @Answer VARCHAR(3) = 'YES'

IF @Answer = 'Yes'
BEGIN 
--Do Something if true
END
ELSE
-- Do Soemthing if false