0

Tab_1

"id"    "name"      "addr"  "country"   "status"
"1"     "norman"    "1st"   "US"        "2"
"2"     "kirk"      "2nd"   "US"        "2"

Tab_2

"id"    "name"      "addr"  "country"   "pos"   "total"
"1"     "norman"    "1st"   "US"        "0"     "0"
"2"     "kirk"      "2nd"   "US"        "0"     "0"

当状态设置为时update,我正在使用下面的Tab_1触发器。问题是,即使它是正确的,它也不会做任何事情。我该如何正确设置?update Tab_2Tab_10

CREATE DEFINER=`root`@`localhost` TRIGGER `tab_2_upd` AFTER UPDATE ON `tab_1` 
FOR EACH ROW BEGIN
if new.status = '0' then
update tab_2 set pos = pos+1, total = total+1 where id = new.id;
//Should it be new.id or old.id? I tried both but still no luck.
end if;
END
4

1 回答 1

1

这是由于您没有更改分隔符而导致多个语句被破坏的另一种情况。这是一个很常见的错误。

尝试这个:

DELIMITER $$

DROP TRIGGER IF EXISTS  `tab_2_upd`$$

CREATE DEFINER=`root`@`localhost` TRIGGER `tab_2_upd` AFTER UPDATE ON `tab_1` 
FOR EACH ROW BEGIN
    if new.status = '0' then
        update tab_2 set pos = pos+1, total = total+1 where id = new.id;
    end if;
END$$

DELIMITER ;

ON UPDATE 触发器同时具有 OLD 和 NEW 值,因为我不认为id不会改变(它不应该),因此使用任何值(OLD 或 NEW)都是安全的。

这里有一个关于分隔符的好答案。

于 2013-10-04T10:34:37.520 回答