2011-01-05 10 views
12

¿Las sentencias condicionales de if/case/when son compatibles con los disparadores de sqlite?sentencia de inserción condicional en los activadores de sqlite

Let `s decir que tengo la siguiente configuración:

CREATE TABLE someTable (id INTEGER PRIMARY KEY, someValue INTEGER); 
CREATE TRIGGER update_another_table AFTER INSERT ON someTable 
BEGIN 
    IF(new.someValue==0) 
     DELETE FROM another_table WHERE (...some condition); 
    ELSE 
     IF NOT EXISTS(SELECT anotherValue FROM another_table WHERE anotherValue =new.someValue) 
      INSERT INTO another_table VALUES(new.someValue, ...); 
     ELSE 
      UPDATE another_table SET anotherValue = new.someValue; 
END; 

pero se eleva un error de sintaxis Sqlite error near 'IF': syntax error"

Respuesta

31

Eso es un error de sintaxis como el diagrama de sintaxis para SQLite disparadores no permite ninguna SI cláusulas ni CASO CUANDO construcciones.

Pero se puede lograr el mismo efecto mediante la definición de dos o tres disparadores que utilizan la condición cuando, ver http://sqlite.org/lang_createtrigger.html

Así se crearía en el gatillo para su caso DELETE así:

CREATE TRIGGER delete_from_other_table AFTER INSERT ON someTable 
WHEN new.someValue = 0 
BEGIN 
    DELETE FROM anotherTable WHERE (... some condition); 
END; 

Y que agregar otro activador para el CASO DE INSERTAR y ACTUALIZAR con las condiciones apropiadas ...

CREATE TRIGGER update_another_table AFTER INSERT ON someTable 
WHEN new.someValue <> 0 
BEGIN 
    ... 
END; 
Cuestiones relacionadas