PLSQL触发器 - 根据选择查询更新记录

时间:2015-06-20 12:49:18

标签: plsql triggers

我有三张桌子:

HouseMode:
mode_id (INT, PK)
switch (CHAR 1BYTE)

ModeDevices:
modedevice_id (INT, PK)
house_mode (INT, FK)
houseroomdevice (INT, FK)

HouseRoomDevices:
houseroomdevice_id (INT, PK)
switch (CHAR 1BYTE)

我希望在更新HouseRoomDevices表中的开关后有一个更新HouseMode表开关的触发器。

我的触发器:

CREATE OR REPLACE TRIGGER switch
BEFORE UPDATE
ON HouseMode
FOR EACH ROW
BEGIN
  UPDATE houseroomdevices
  SET switch = :NEW.switch
  WHERE EXISTS(SELECT houseroomdevice_id FROM houseroomdevices INNER JOIN modedevices ON houseroomdevice = houseroomdevice_id WHERE house_mode = :NEW.mode_id);
END;

但是当我尝试更新记录时:

UPDATE HouseMode
SET switch = 1
WHERE mode_id = 1;

它会更新HouseRoomDevices表中的所有记录。

1 个答案:

答案 0 :(得分:1)

您的更新语句错误,因为您的WHERE条件始终为true。使用这个:

UPDATE houseroomdevices
SET switch = :NEW.switch
WHERE houseroomdevice_id IN  (SELECT houseroomdevice FROM modedevices WHERE house_mode = :NEW.mode_id);