存储过程实例,对应表的操作记录

对应表下创建触发器:

up-3f6e2cb712d4fbb147bdc4ccad467ad94b2.png

up-36a16393faa0aa6c56224b3a7d64183ee8a.png

begin
insert into staff_update_record (staff_id,old_info,new_info,created_at,type) values(new.id,'新添加','新添加',CURRENT_TIMESTAMP(),1);

end

 

BEGIN
call staff_update(new.id,new.department_id,old.department_id,new.center_id,old.center_id,new.store_id,old.store_id,new.status,old.status,new.positive,old.positive,new.employ_date,old.employ_date,new.is_on_circulation,old.is_on_circulation,new.is_seven_days,old.is_seven_days);
END

函数中写好自己的存储过程:

up-6002a6d89689444376cb9aa396f0af32101.png

up-21f3492aa787048bcd2801835ed2ced6c5e.png

CREATE DEFINER=CURRENT_USER PROCEDURE `staff_update`(IN `staff_id` int,IN `new_department_id` int,IN `old_department_id` int,IN `new_center_id` int,IN `old_center_id` int,IN `new_store_id` int,IN `old_store_id` int,IN `new_status` int,IN `old_status` int,IN `new_positive` int,IN `old_positive` int,IN `new_employ_date` int,IN `old_employ_date` int,IN `new_is_on_circulation` int,IN `old_is_on_circulation` int,IN `new_is_seven_days` int,IN `old_is_seven_days` int)
BEGIN

declare old_department_write VARCHAR(32) default '无'; 
declare new_department_write VARCHAR(32) default '无';

declare old_center_write VARCHAR(32) default '无';
declare new_center_write VARCHAR(32) default '无';

declare old_store_write VARCHAR(32) default '无';
declare new_store_write VARCHAR(32) default '无';

declare new_status_write VARCHAR(32) default '无';
declare old_status_write VARCHAR(32) default '无';

declare new_positive_write VARCHAR(32) default '无';
declare old_positive_write VARCHAR(32) default '无';

declare new_employ_date_write VARCHAR(32) default '无';
declare old_employ_date_write VARCHAR(32) default '无';

declare new_is_on_circulation_write VARCHAR(32) default '无';
declare old_is_on_circulation_write VARCHAR(32) default '无';

declare new_is_seven_days_write VARCHAR(32) default '无';
declare old_is_seven_days_write VARCHAR(32) default '无';

IF new_department_id <> old_department_id OR new_center_id <> old_center_id OR new_store_id <> old_store_id THEN

        select CONCAT_WS('-',store.store_name,center.center_name,CONCAT(department.department_number,'部')) as old into old_department_write from department LEFT JOIN department_relation on department_relation.department_id = department.id LEFT JOIN center on department_relation.center_id = center.id LEFT JOIN store on department_relation.store_id = store.id where department.id = old_department_id;
        
        select CONCAT_WS('-',store.store_name,center.center_name,CONCAT(department.department_number,'部')) as new into new_department_write from department LEFT JOIN department_relation on department_relation.department_id = department.id LEFT JOIN center on department_relation.center_id = center.id LEFT JOIN store on department_relation.store_id = store.id where department.id = new_department_id;
        
        insert into staff_update_record (staff_id,old_info,new_info,created_at,type) values(staff_id,old_department_write,new_department_write,CURRENT_TIMESTAMP(),2);
END IF;


IF new_status <> old_status THEN

    IF new_status = 0 THEN
      set new_status_write = '在职';
    ELSEIF new_status = 1 THEN
        set new_status_write = '离职';
    ELSE
        set new_status_write = '停职';
    END IF;
    
    IF old_status = 0 THEN
      set old_status_write = '在职';
    ELSEIF old_status = 1 THEN
        set old_status_write = '离职';
    ELSE
        set old_status_write = '停职';
    END IF;
    
    insert into staff_update_record (staff_id,old_info,new_info,created_at,type) values(staff_id,old_status_write,new_status_write,CURRENT_TIMESTAMP(),4);
END IF;
    
    
IF new_positive <> old_positive THEN

    IF new_positive = 0 THEN
      set new_positive_write = '未转正';
    ELSE
        set new_positive_write = '转正';
    END IF;
    
    IF old_positive = 0 THEN
      set old_positive_write = '未转正';
    ELSE
        set old_positive_write = '转正';
    END IF;
    
    insert into staff_update_record (staff_id,old_info,new_info,created_at,type) values(staff_id,old_positive_write,new_positive_write,CURRENT_TIMESTAMP(),5);
END IF;
    

IF new_employ_date <> old_employ_date THEN

    set new_employ_date_write = FROM_UNIXTIME(new_employ_date,'%Y-%m-%d %H:%i:%S');
    set old_employ_date_write = FROM_UNIXTIME(old_employ_date,'%Y-%m-%d %H:%i:%S');
    
    insert into staff_update_record (staff_id,old_info,new_info,created_at,type) values(staff_id,old_employ_date_write,new_employ_date_write,CURRENT_TIMESTAMP(),6);
END IF;


IF new_is_on_circulation <> old_is_on_circulation THEN

    IF new_is_on_circulation = 0 THEN
      set new_is_on_circulation_write = '开启';
    ELSE
        set new_is_on_circulation_write = '关闭';
    END IF;
    
    IF old_is_on_circulation = 0 THEN
      set old_is_on_circulation_write = '开启';
    ELSE
        set old_is_on_circulation_write = '关闭';
    END IF;
    
    insert into staff_update_record (staff_id,old_info,new_info,created_at,type) values(staff_id,old_is_on_circulation_write,new_is_on_circulation_write,CURRENT_TIMESTAMP(),7);
END IF;


IF new_is_seven_days <> old_is_seven_days THEN

    IF new_is_seven_days = 0 THEN
      set new_is_seven_days_write = '否';
    ELSE
        set new_is_seven_days_write = '是';
    END IF;
    
    IF old_is_seven_days = 0 THEN
      set old_is_seven_days_write = '否';
    ELSE
        set old_is_seven_days_write = '是';
    END IF;
    
    insert into staff_update_record (staff_id,old_info,new_info,created_at,type) values(staff_id,old_is_seven_days_write,new_is_seven_days_write,CURRENT_TIMESTAMP(),8);
END IF;


END

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值