我正在编写一个DML触发器,用于在新表中插入更新行。只有当一个列被更新时触发器才会触发,但是...可以在UP_sATE OF one_single_column子句之外的其他列中使用吗?
审核表:
CREATE TABLE employees_salary_history(
user_name VARCHAR2(45) NOT NULL,
time_stamp date DEFAULT (sysdate),
employee_id NUMBER(6),
old_salary NUMBER(8, 2),
new_salary NUMBER(8, 2),
percente_raise_salary NUMBER(8, 2));
DML TRIGGER:
CREATE OR REPLACE TRIGGER update_salary_trg
AFTER
UPDATE
OF salary
ON employees23
FOR EACH ROW
DECLARE
v_user VARCHAR2(25);
BEGIN
SELECT user
INTO v_user
FROM dual;
INSERT INTO employees_salary_history
VALUES(
v_user,
sysdate,
employee_id, -- I would like add employee_id about employee that was updated,
:OLD.salary,
:NEW.salary,
TRUNC((:NEW.salary - :OLD.salary) / :OLD.salary * 100, 3)
);
END update_salary_trg;
当我省略“employee_id”列时,触发器正常工作,但我想添加employee_id作为更新工资的参考。当我添加employee_id列时,我收到以下错误:
Errors: check compiler log
Errores para TRIGGER HR.UPDATE_SALARY_TRG:
LINE/COL ERROR
-------- ----------------------------------
8/3 PL/SQL: SQL Statement ignored
8/15 PL/SQL: ORA-00913: too many values
答案 0 :(得分:1)
是的,可以在触发器中使用任何employees23-table列。
如果列未更改,则:old
和:new
伪记录的值相同。
因此,在您的情况下,只需使用:new.employee_id
。
接下来,我建议您使用RTFM:PL/SQL Triggers。它还详细解释了伪记录何时可用以及它们保持的值。
答案 1 :(得分:1)
这是因为触发器无法引用employee_id字段。在您的情况下,New和OLD将是相同的。因此,只需将employee_id替换为:OLD.employee_id或:NEW。 employee_id如下所示。希望它有所帮助。
CREATE OR REPLACE TRIGGER update_salary_trg
AFTER
UPDATE
OF salary
ON employees23
FOR EACH ROW
DECLARE
v_user VARCHAR2(25);
BEGIN
SELECT user
INTO v_user
FROM dual;
INSERT INTO employees_salary_history --employees_salary_history
VALUES(
v_user,
sysdate,
:NEW.employee_id, -- I would like add employee_id about employee that was updated,
:OLD.salary,
:NEW.salary,
TRUNC((:NEW.salary - :OLD.salary) / :OLD.salary * 100, 3)
);
END update_salary_trg;