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

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -