IIF (...) no es una función incorporada reconocida


Estoy tratando de usar esto en Microsoft SQL Server 2008 R2:

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

Pero obtengo un error:

IIF(...) no es un nombre de función incorporado reconocido

Es IIF() solo compatible con una versión posterior?

¿Hay una función alternativa que pueda usar?

Author: Danny Beckett, 2012-08-20

4 answers

Como otros han dicho, IIF viene de SQL 2012. Antes de eso, puede utilizar CASE:

SET @SomeVar = @SomeOtherVar + CASE
 WHEN @SomeBool
 THEN 'value when true'
 ELSE 'value when false'
END
 41
Author: Richard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-23 23:13:16

Qué hay de nuevo en SQL Server 2012, Mejoras de programabilidad :

SQL Server 2012 presenta 14 nuevas funciones integradas. Estas funciones facilitan el camino de migración para los trabajadores de la información al emular la funcionalidad que se encuentra en los lenguajes de expresión de muchas aplicaciones de escritorio. Sin embargo, estas funciones también serán útiles para los usuarios experimentados de SQL Server.

...

 8
Author: Damien_The_Unbeliever,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-20 09:26:54

IIF es no válido para SQL Server 2008 R2 y cualquier versión anterior.

IIF se introdujo en SQL Server 2012 (no hay enlace a versiones anteriores en la página de documentación a la que he vinculado).

 4
Author: Oded,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-20 09:25:12

También podría usar la instrucción IF estándar si está fuera de select.

Por ejemplo

DECLARE @Answer VARCHAR(3) = 'YES'

IF @Answer = 'Yes'
BEGIN 
--Do Something if true
END
ELSE
-- Do Soemthing if false
 4
Author: Will Wainwright,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-23 05:09:15