我有两个表,t1 和 t2。 我正在尝试创建一个触发器,以便当 t1 获得更新时,我检查更新的内容是否为 t1.nStatus。
如果 t1.nStatus = 2,我需要为所有等于 t1.nId 的 t2.customer_Id 设置 t2.bEnabled = 1
CREATE OR REPLACE TRIGGER change AFTER UPDATE ON t1
FOR EACH ROW
BEGIN
IF NEW.nStatus = 2 THEN
UPDATE t2 SET bEnabled = 1 WHERE t2.immobile_id = NEW.nId;
END IF;
END;
我得到的错误:
1 queries executed, 0 success, 1 errors, 0 warnings
Query: CREATE OR REPLACE trigger changeStatusImmobile after UPDATE on immobile for each row begin IF NEW.nStatus = 2 then UPDATE select...
Error Code: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 5
Execution Time : 0 sec Transfer Time : 0 sec Total Time : 0.146 sec
你有什么想法吗?
最佳答案
尝试设置分隔符
DELIMITER $$
CREATE OR REPLACE TRIGGER change AFTER UPDATE ON t1
FOR EACH ROW
BEGIN
IF NEW.nStatus = 2 THEN
UPDATE t2 SET bEnabled = 1 WHERE t2.immobile_id = NEW.nId;
END IF;
END$$
DELIMITER ;
关于mysql - 如何使用触发器mysql更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57115116/