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