简体   繁体   中英

After insert trigger in mysql

I am trying to write a trigger in mysql that runs after insert in table. After insert it takes a value from a column attrValue where attrType equals 'datetime' and updates another column called attrMd.

This is my try for now but it keeps saying I have a syntax value in line 4:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4

I can't figure it out. Any help is much appreciated. Thanks...

CREATE TRIGGER upd_check BEFORE INSERT ON def_servpath_0001_weatherstation
FOR EACH ROW
BEGIN
 DECLARE someString text;
 FOR EACH ROW
 BEGIN
     IF NEW.atrType = 'datetime' THEN
         SET @someString := NEW.atrValue;
     END IF;
 END;

 FOR EACH ROW
 BEGIN
     IF NEW.atrType <> 'datetime' THEN
         SET NEW.atrMD = someString;
     END IF;
 END;
END;//

Edit 图片

Some points to consider:

CREATE TRIGGER upd_check BEFORE INSERT ON def_servpath_0001_weatherstation
FOR EACH ROW
BEGIN
 DECLARE someString text;
 -- FOR EACH ROW
 BEGIN
     IF NEW.atrType = 'datetime' THEN
         SET @someString := NEW.atrValue;
     END IF;
 END;

 -- FOR EACH ROW
 BEGIN
     IF NEW.atrType <> 'datetime' THEN
         SET NEW.atrMD = someString;
     END IF;
 END;
END;//

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM