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:
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;//
Some points to consider:
DELIMITER
option in phpMyAdmin. @someString
) and local variables 13.6.4.1 Local Variable DECLARE Syntax ( someString
), are different variables. FOR EACH ROW
). 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.