【问题标题】:How to do a nested when in a trigger in sql?如何在sql中的触发器中进行嵌套?
【发布时间】:2019-12-02 01:29:12
【问题描述】:

这句话有什么问题?它说我在“何时”收到错误,我需要检查以确保没有外键问题,之后,我需要检查外键问题是否来自错误表中输入数据的错误。

CREATE TRIGGER trigger3
            BEFORE INSERT ON sightings
            FOR EACH ROW
                WHEN ((SELECT flowers.comname FROM flowers WHERE comname = NEW.name) IS NULL) 
                BEGIN
                    WHEN ((SELECT genus, species FROM flowers WHERE CONCAT(species,' ',genus) = NEW.name) IS NOT NULL)
                    THEN
                        INSERT INTO sightings VALUES (comname, NEW.person, NEW.location, NEW.sighted)
                    END IF;
                END; 
            END;

【问题讨论】:

    标签: sql triggers insert nested case-when


    【解决方案1】:

    使用IF 而不是WHEN

    CREATE TRIGGER trigger3
                BEFORE INSERT ON sightings
                FOR EACH ROW
                    BEGIN
                        IF ((SELECT flowers.comname FROM flowers WHERE comname = NEW.name) IS NULL) 
                        BEGIN
                            IF ((SELECT genus, species FROM flowers WHERE CONCAT(species,' ',genus) = NEW.name) IS NOT NULL)
                            BEGIN
                                INSERT INTO sightings VALUES (comname, NEW.person, NEW.location, NEW.sighted)
                            END ;
                        END; 
                    END;
                END;
    

    【讨论】:

    • @BrandonWand,再试一次
    • 不起作用,可能与我使用的是sqlite有关?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2014-06-07
    • 2015-10-19
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    相关资源
    最近更新 更多