2

Unable to create trigger on update of specific field

delimiter //
CREATE TRIGGER `add_event` AFTER  UPDATE ON `order_table` 
  FOR EACH ROW 
  IF NEW.status <=> OLD.status THEN
    INSERT INTO `events` SET 
      events.status = NEW.status, 
      events.order_id = NEW.order_id, 
      events.time_stamp = CURRENT_TIMESTAMP
  END IF;
delimiter ;

I am getting this error

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 'END IF;

9
  • You shouldn't have , INSERT after UPDATE Commented Aug 6, 2018 at 9:05
  • No such thing as and AFTER UPDATE, INSERT trigger - you need one of each. Commented Aug 6, 2018 at 9:05
  • Now I just think you need a ; after CURRENT_TIMESTAMP to terminate your statement properly. Commented Aug 6, 2018 at 9:12
  • 2
    after FOR EACH ROW you need a begin key word and after end if;you Need an end;// Commented Aug 6, 2018 at 9:13
  • SET statement has no effect in an AFTER trigger because the row change will have already occurred Commented Aug 6, 2018 at 9:15

3 Answers 3

1

There is no syntax error in below mysql trigger.

delimiter //
 CREATE TRIGGER add_event AFTER UPDATE ON order_table
    FOR EACH ROW
    BEGIN
         IF NEW.status <=> OLD.status THEN
            INSERT INTO `events` 
            SET events.status = NEW.status, 
            events.order_id = NEW.order_id, 
            events.time_stamp = CURRENT_TIMESTAMP;
         END IF;
     END;//
 delimiter ;
Sign up to request clarification or add additional context in comments.

2 Comments

Works!! Thankyou :)
@deadman Ananth can't upvote, doesn't have enough rep points. I did though.
1
drop table if exists t;
drop table if exists events;

create table t(order_id int, status varchar(1));
create table events(order_id int, time_stamp timestamp,status varchar(1));
drop trigger if exists t;
delimiter //
CREATE TRIGGER t after update ON `t` 
 FOR EACH ROW 
 begin
 IF NEW.status <> OLD.status THEN
    INSERT INTO `events` 
        SET events.status = NEW.status, 
                events.order_id = NEW.order_id, 
                 events.time_stamp = CURRENT_TIMESTAMP;
END IF ;
end //
 delimiter ;

 insert into t values(1,1);
 insert into events values(1,now(),1);
 update t set status = 2 where order_id = 1;
 select * from t;
 select * from events;

MariaDB [sandbox]>  select * from t;
+----------+--------+
| order_id | status |
+----------+--------+
|        1 | 2      |
+----------+--------+
1 row in set (0.00 sec)

MariaDB [sandbox]>  select * from events;
+----------+---------------------+--------+
| order_id | time_stamp          | status |
+----------+---------------------+--------+
|        1 | 2018-08-06 10:16:35 | 1      |
|        1 | 2018-08-06 10:16:35 | 2      |
+----------+---------------------+--------+
2 rows in set (0.00 sec)

Comments

0

Why are you using if statement. You are checking everything <=>. Here is the solution :

CREATE TRIGGER `add_event` AFTER  UPDATE ON `order_table` 
 FOR EACH ROW 
 BEGIN
 INSERT INTO `events` SET events.status = NEW.status, events.order_id = NEW.order_id, events.time_stamp = CURRENT_TIMESTAMP;
END;
 delimiter ;

3 Comments

Even I need to check if paricular field is updated, Insert if updated
which version you are using?
You are checking everything. This IF statement always return true.

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.