-1

在这里,我想将 X 表的内容复制到 Y 表中,并且在同一行上,我想从 X 表中删除该行。我按以下方式尝试了它,但它不起作用。

Drop trigger if exists myTrigger;

delimiter |
create trigger myTrigger 
after insert on X 
for each row
BEGIN
    IF STRCMP(NEW.SysLogTag,"kernel:") = 0 THEN
        INSERT INTO Y(logtime,moduleid,severity,messageid,message) values(NULL,1,1,100,NEW.Message);
    ELSEIF NEW.SysLogTag like 'ntpd[%]:' THEN
        INSERT INTO Y(logtime,moduleid,severity,messageid,message) values(NULL,6,1,100,NEW.Message);
    ELSE
         INSERT INTO Y(logtime,moduleid,severity,messageid,message) values(NULL,4,1,100,NEW.Message);
    END IF;
    delete from X where ID=NEW.ID; //Not working.
END;|
delimiter ;
4

1 回答 1

0

来自http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html#c8808

我的解决方案是在要插入的表上创建一个唯一/主键(在本例中为 S_ID),然后如果我的完整性检查失败,我将 s_id 更改为 0。此方法只会创建一个带有s_id 为 0。显然你需要 INSERT 忽略!

CREATE TRIGGER sanityCheck
BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
IF something THEN
SET NEW.S_ID = 0 ;
END IF;
END;
于 2013-04-22T05:25:54.183 回答