sql - Syntax error on MySQL trigger when only updating one column -


i'm trying create mysql trigger fires when column user_email updated in table wp_users, it's failing. i'm getting syntax error before second begin, looks fine me. missing?

create trigger `email_update`  after update on `wp_users`  each row  begin if new.user_email <> old.user_email     begin          insert `bfintranet`.`intranet_staff_updates` (`staff_update_id`, `user_id`, `updated_value`, `new_value`, `old_value`, `update_datetime`)          values (null, new.id, 'user_email', new.user_email, old.user_email, now())      end  end; 

without 1 column condition, works fine (but doesn't achieve want to):

create trigger `email_update`  after update on `wp_users`  each row  insert `bfintranet`.`intranet_staff_updates` (`staff_update_id`, `user_id`, `updated_value`, `new_value`, `old_value`, `update_datetime`) values (null, new.id, 'user_email', new.user_email, old.user_email, now()) 

it appears error on whatever comes after if, regardless of comes after or if condition is.

you have couple of problems.

first should review mysql if...then...end if syntax: https://dev.mysql.com/doc/refman/5.5/en/if.html

also complex queries need temporarily override default delimiter can use semi-colons within trigger.

this should work:

delimiter $$  create trigger `email_update`  after update on `wp_users`  each row  begin if new.user_email <> old.user_email              insert `bfintranet`.`intranet_staff_updates` (`staff_update_id`, `user_id`, `updated_value`, `new_value`, `old_value`, `update_datetime`)          values (null, new.id, 'user_email', new.user_email, old.user_email, now());     end if; end $$  delimiter ; 

Comments

Popular posts from this blog

gcc - MinGW's ld cannot perform PE operations on non PE output file -

timeout - Handshake_timeout on RabbitMQ using python and pika from remote vm -

c# - Search and Add Comment with OpenXML for Word -