mysql创建触发器

注:触发器中不能调用存储过程,触发器功能应尽量简单

use d_database_name;-- 切换到数据库
set NAMES 'utf8';

-- drop if exists when update can use
drop trigger if exists tr_update_bind_sno;

delimiter //

create trigger tr_update_bind_sno
after update on t_order_19
for each row
begin

  -- 用户注册手机号
  declare v_cellphone varchar(16);

  -- old记录更新前的状态,new代表更新后的数据
  if old.c_bind_sno<>new.c_bind_sno
     && length(old.c_bank_card)>0
  then
     -- 获取用户注册手机号
     select c_cellphone into v_cellphone from t_user
     where c_user_id=c_user_id;

     -- 更新t_channel_account的绑卡标示和预留银行手机号
     update t_channel_account
     set c_bind_number=old.c_bind_sno
     ,c_bank_cellphone=old.c_bank_cellphone
     where c_user_id=old.c_user_id;

  end if;
end //

delimiter ;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章