我想为每一行输出OLD和New薪水值,但不断收到变异表错误。
这是我的触发器代码:
CREATE OR REPLACE TRIGGER print_salary_changes
AFTER UPDATE OF salary ON emp2
FOR EACH ROW
DECLARE
v_employee_id NUMBER;
v_old_salary NUMBER;
v_new_salary NUMBER;
v_difference NUMBER;
BEGIN
SELECT :OLD.employee_id, :OLD.salary, :NEW.salary INTO v_employee_id, v_old_salary, v_new_salary FROM emp2;
v_difference := v_old_salary - v_new_salary;
DBMS_OUTPUT.PUT_LINE ('employee_id: ' || v_employee_id);
DBMS_OUTPUT.PUT_LINE ('old salary: ' || v_old_salary );
DBMS_OUTPUT.PUT_LINE ('new salary: ' || v_new_salary);
DBMS_OUTPUT.PUT_LINE ('DIFFERENCE: ' || v_difference);
END;
答案 0 :(得分:2)
您不需要执行select语句,您的触发器将具有旧值和新值。
以这种方式尝试:
CREATE OR REPLACE TRIGGER print_salary_changes
AFTER UPDATE OF salary ON emp2
FOR EACH ROW
BEGIN
DBMS_OUTPUT.PUT_LINE ('employee_id: ' || :OLD.employee_id);
DBMS_OUTPUT.PUT_LINE ('old salary: ' || :OLD.salary );
DBMS_OUTPUT.PUT_LINE ('new salary: ' || :NEW.salary);
DBMS_OUTPUT.PUT_LINE ('DIFFERENCE: ' || (:OLD.salary -:NEW.salary ));
END;
答案 1 :(得分:1)
这是一种预期的正常行为,Oracle希望保护您: