4

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

0

1 Answer 1

7

you are missing SET

   IF (NEW.email = OLD.email) THEN
      SET email = NULL;
   ELSE
      SET email = OLD.email;
   END IF;
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome. Thanks Bro. I really love my Stackoverflow family that is always up for selfless help.
Sorry I am bothering you again. Since I am using AFTER UPDATE trigger, it is not letting me to update column value of the table that is being triggered.
If I have three conditions, what should I do ? I need to elseif, there is elseif in the mysql (trigger) ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.