更新工资时计算和打印新旧工资差异的触发器

时间:2017-11-21 12:17:34

标签: mysql triggers

员工表定义为员工(eno,ename,salary)。

写一个触发器来计算并打印工资更新时新旧薪水之间的差异。

我尝试了以下触发器。它似乎计算出差异。如何打印?

DELIMITER $$
CREATE TRIGGER t1
AFTER UPDATE ON employee
FOR EACH ROW
BEGIN   
    DECLARE sal_diff DECIMAL(10,2);
    IF eno = new.eno THEN
        IF (new.salary < old.salary) THEN
            SET sal_diff = old.salary - new.salary;
        ELSE
            SET sal_diff = new.salary - old.salary;
        END IF;
    END IF;
END $$
DELIMITER ;

1 个答案:

答案 0 :(得分:0)

类似的东西:

DELIMITER $$
CREATE TRIGGER t1
AFTER UPDATE ON employee
FOR EACH ROW
BEGIN   
    DECLARE sal_diff DECIMAL(10,2);
    IF new.salary < old.salary THEN
       set @sal_diff = old.salary - new.salary;
    ELSE
       set @sal_diff = new.salary - old.salary;
    END IF;
   -- Use  @sal_diff where you want it;
END $$
DELIMITER ;