SQLite更新触发器问题

时间:2015-10-21 18:48:08

标签: sqlite

所以我试图使用SQLite Manager来操作数据库。 我试图创建一个更新字段的触发器,基于另一个字段被更改,这就是我目前所拥有的

CREATE TRIGGER staff_update
AFTER UPDATE OF c_doctor ON tickets
WHEN OLD.c_doctor <> NEW.c_doctor
BEGIN
    UPDATE tickets
    SET c_doctor_staff_contact =  doctor_staff_contact.c_doctor_staff_contact 
    WHERE NEW.c_doctor = doctor_staff_contact.c_doctor;
END

c_doctor是ticket表中字段的名称以及doctor_staff_contact表

doctor_staff_contact也是这些表中字段的名称

重复的命名约定是为了方便我记住

我已经使用SET c_doctor_staff_contact = "test"; END进行了尝试,并且效果很好,所以我的假设= doctor_staff_contact.c_doctor_staff_contact WHERE NEW.c_doctor = doctor_staff_contact.c_doctor; END给了我一些问题

我一直在SQLite.org上查找语法,据我所知,我做得对。

更新

= c_doctor_staff_contact IN (SELECT c_doctor_staff_contact FROM doctor_staff_contact
    WHERE NEW.c_doctor = doctor_staff_contact.c_doctor);
END

所以这个“有效”,但这样做会将c_doctor_staff_contact更改为0,并将所有其他票证c_doctor_staff_contact更改为0

解决方案:

CREATE TRIGGER staff_update
AFTER UPDATE OF c_doctor ON tickets
WHEN OLD.c_doctor <> NEW.c_doctor
BEGIN
    UPDATE tickets
    SET  c_doctor_staff_contact = 
       (SELECT c_doctor_staff_contact 
       FROM doctor_staff_contact
       WHERE NEW.c_doctor = doctor_staff_contact.c_doctor)
    WHERE id = OLD.id;
END

1 个答案:

答案 0 :(得分:0)

触发器主体中的UPDATE查询应如下所示:

CREATE TRIGGER staff_update
AFTER UPDATE OF c_doctor ON tickets
WHEN OLD.c_doctor <> NEW.c_doctor
BEGIN
    UPDATE tickets
    SET c_doctor_staff_contact = (
        SELECT c_doctor_staff_contact 
        FROM doctor_staff_contact
        WHERE NEW.c_doctor = c_doctor
        )
    WHERE NEW.c_doctor = c_doctor;
END;