4
votes

Trying to execute first ever trigger which should insert value of one table to another only if the data is new. Below is my code:

BEGIN
    DECLARE email VARCHAR(30);
   INSERT INTO data_audit SET data_audit_id = OLD.id;
   IF (NEW.email = OLD.email) THEN
    email = NULL;
   ELSE
    email = OLD.email;
   END IF;
   UPDATE corporate_audit SET email = email WHERE corporate_audit_id = last_insert_id();
END

I am getting following error:

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 '= NULL; ELSE email = OLD.email; END IF; UPDATE corporate_audit SET ' at line 5

And when I execute code without any IF-THEN block and store OLD.email then it just insert new ID in audit table but does not update field. Please guide me

1

1 Answers

7
votes

you are missing SET

   IF (NEW.email = OLD.email) THEN
      SET email = NULL;
   ELSE
      SET email = OLD.email;
   END IF;