2010-12-07 16 views
6

¿Existe la instrucción IIF en todas las versiones de SQL Server?instrucción IIF en SQL Server 2005

He revisado un tutorial en MSDN.

Pero cuando traté de ejecutar este código en mi máquina

DECLARE @newDate datetime 
SET @newDate = CONVERT(varchar, {fn NOW()}, 111) 
SELECT IIF(@newDate > '2010/12/2', 'Greater', 'smaller') 

Pero estoy consiguiendo error de "sintaxis incorrecta cerca de '>'."

¿Alguien me puede dar un ejemplo en SQL Server 2005 para la existencia de la declaración IIF?

Respuesta

11

Eso IIF declaración sólo existe en MDX - el lenguaje de consulta de SQL Server Analysis Servicios - Datawarehousing el lado de SQL Server.

Plain T-SQL does no tienen una declaración IIF.

Lo mejor que puede hacer en T-SQL es usar la declaración CASE.... WHEN... THEN....

+3

ACTUALIZACIÓN: SQL Server 2012 ahora tiene una declaración IIF. http://msdn.microsoft.com/en-us/library/hh213574.aspx – Somantra

+0

@Somantra: cierto, pero no se conocía en diciembre de 2010, y el OP preguntaba por SQL Server 2005. –

+3

El título dice 2005, pero su primera pregunta fue "¿Existe una declaración IIF en todas las versiones de SQL Server?". Esperemos que alguien encuentre algo útil en esta publicación en el futuro. ;-) – Somantra

6

Usted es mejor usar una expresión CASE:

DECLARE @newDate datetime 
SET @newDate = CONVERT(varchar, {fn NOW()}, 111) 
SELECT CASE WHEN @newDate > '20101202' THEN 'Greater' ELSE 'smaller' END 

También observe por favor que he cambiado su fecha literal a un formato seguro - '02/12/2010' podría ser interpretado por el servidor SQL como el 12 de febrero o el 2 de diciembre.

-1
IIF([Add DVP Month].DevelopmentMonth>[Add DVP Month].COVMONTHS, 
     1, 
    IIF([STATUS]<>'1', 
     1, 
    IIF([Add DVP Month].PLANTYPE = 'A' and [Add DVP Month].newUsedProgram = 'U' and [Add DVP Month].COVMONTHS = 60 and [Add DVP Month].COVMILES = 100000 and [Add DVP Month].postedDt >= #1/31/2010#, 
    IIF([Add DVP Month].postedDt >= #1/31/2012#, 
     [EPMthd.PCM2], 
    IIF([Add DVP Month].postedDt >= #1/31/2010#, 
    [EPMthd.PCM1], 
    [EPMthd.PCM0]) 
    ), 
    IIF([Add DVP Month].COVMONTHS = 999,[EPMthd.2], 
    IIF([Add DVP Month].postedDt >= #1/31/2012#, [EPMthd.2], 
    IIF([Add DVP Month].postedDt >= #1/31/2010#, [EPMthd.1], 
    IIF([Add DVP Month].postedDt >= #1/31/2008#, 
    IIF([EPMthd.0] is null, 
     [EPMthd.8], 
     [EPMthd.0] 
    ), 
    IIF([Add DVP Month].postedDt < #1/31/2008#, 
    IIF([EPMthd.8] is null, 
    IIF([Add DVP Month].COVMONTHS = 0,0, 
     [Add DVP Month].DevelopmentMonth/[Add DVP Month].COVMONTHS 
    ), 
     [EPMthd.8] 
    ), 
    IIF([Add DVP Month].COVMONTHS = 0, 
    0, 
     [Add DVP Month].DevelopmentMonth/[Add DVP Month].COVMONTHS 
    ) 
    ) 
)))))) 
) AS [EP%] 
+1

Esto es solo un mensaje de basura. – nalply

+0

De acuerdo. No es lo que se preguntó y la sintaxis totalmente diferente a SQL Server – Fandango68

Cuestiones relacionadas