sqlite中是否支持条件if / case / when语句?
让我们说我有以下设置:
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;
这是一个语法错误,因为sqlite触发器的语法图不允许任何IF子句或CASE WHEN结构.
但是您可以通过定义使用WHEN条件的两个或三个触发器来实现相同的效果,请参见http://sqlite.org/lang_createtrigger.html
所以你会创建一个触发器为您的DELETE案例:
CREATE TRIGGER delete_from_other_table AFTER INSERT ON soMetable WHEN new.someValue = 0 BEGIN DELETE FROM anotherTable WHERE (... some condition ); END;
而不是添加另一个触发器为INSERT和UPDATE案例与适当的条件…
CREATE TRIGGER update_another_table AFTER INSERT ON soMetable WHEN new.someValue <> 0 BEGIN ... END;