admin

在SQLite触发器中测试COUNT()> X

sql

如何在SQLite的触发器中比较COUNT的结果?

到目前为止,这是我想出的代码:

CREATE TRIGGER mytrigger
BEFORE INSERT ON mytable
BEGIN
    SELECT CASE WHEN 
        SELECT COUNT (*) FROM mytable >= 3
    THEN
        RAISE(FAIL, "Activated - mytrigger.")
    END;
END;

它无法使用以下命令进行编译:

Error: near "SELECT": syntax error

如果我替换SELECT COUNT (*) FROM mytable >= 31 == 1,它将编译良好,并且触发器将始终执行。


阅读 136

收藏
2021-07-01

共1个答案

admin

您需要在整个SELECT语句周围添加括号 SELECT COUNT (*) FROM mytable

CREATE TRIGGER mytrigger
BEFORE INSERT ON mytable
BEGIN
    SELECT CASE WHEN 
        (SELECT COUNT (*) FROM mytable) >= 3
    THEN
        RAISE(FAIL, "Activated - mytrigger.")
    END;
END;
2021-07-01