Multiple argument IF statement-T-SQL


¿Cómo escribo una sentencia IF con múltiples argumentos en T-SQL?

Error de fuente actual:

DECLARE @StartDate AS DATETIME
DECLARE @EndDate AS DATETIME

SET @StartDate = NULL
SET @EndDate = NULL

IF (@StartDate IS NOT NULL AND @EndDate IS NOT NULL) 
    BEGIN
        -- do some work
    END

Arroja el siguiente error:

Sintaxis incorrecta cerca de la palabra clave "Y". Sintaxis incorrecta cerca de la palabra clave 'Y'. Sintaxis incorrecta cerca ')'.

 23
Author: Michael Kniskern, 2009-12-28

5 answers

Lo estás haciendo bien. El bloque de código vacío es lo que está causando su problema. No es la estructura de la condición :)

DECLARE @StartDate AS DATETIME

DECLARE @EndDate AS DATETIME

SET @StartDate = NULL
SET @EndDate = NULL

IF (@StartDate IS NOT NULL AND @EndDate IS NOT NULL) 
    BEGIN
        print 'yoyoyo'
    END

IF (@StartDate IS NULL AND @EndDate IS NULL AND 1=1 AND 2=2) 
    BEGIN
        print 'Oh hey there'
    END
 39
Author: Shaun F,
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
2009-12-28 20:08:27

Esa es la forma de crear expresiones booleanas complejas: combinarlas con AND y OR. El fragmento que publicaste no arroja ningún error para el IF.

 1
Author: Remus Rusanu,
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
2009-12-28 20:07:03

Parece funcionar bien.

Si tiene un BEGIN vacío ... FIN bloque que podría ver

Msg 102, Nivel 15, Estado 1, Línea 10 Sintaxis incorrecta cerca de 'END'.

 1
Author: Adriaan Stander,
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
2009-12-28 20:07:15

No está seguro de cuál es el problema, esto parece funcionar bien?

DECLARE @StartDate AS DATETIME
DECLARE @EndDate AS DATETIME

SET @StartDate = NULL
SET @EndDate = NULL

IF (@StartDate IS NOT NULL AND @EndDate IS NOT NULL) 
    BEGIN
        Select 'This works just fine' as Msg
    END
Else
    BEGIN
    Select 'No Lol' as Msg
    END
 1
Author: kd7,
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
2009-12-28 20:07:16

Su código es válido (con una excepción). Se requiere tener código entre el INICIO y el FINAL.

Sustitúyase

--do some work

Con

print ''

Creo que tal vez viste "FIN y no" Y "

 1
Author: Gabriel McAdams,
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
2009-12-28 20:08:14