我有三个具有以下各列的表:
将行插入WorkingOn表时,我试图在Oracle中创建行级触发器。它将获得项目预算的总和,并更新为员工正在从事的员工的total_project_budget。
我当前的行级别触发器:
CREATE OR REPLACE TRIGGER testtrigger
AFTER INSERT ON WorkingOn
FOR EACH ROW
BEGIN
UPDATE employee
SET TOTAL_BUDGET = ( SELECT SUM(budget)
FROM project INNER JOIN workingon
ON project.project_id = workson.project_id
WHERE workingon.employee_id = employee.employee_id )
WHERE employee_id = :NEW.employee_id;
END;
/
如果运行此触发器,我会得到一个突变表,
ORA-04091:表用户。WORKINGON正在变异,触发器/函数可能看不到
但是如果我使用AUTONOMOUS_TRANSACTION,
ORA-06519:检测到活动的自主事务并回滚
编辑:
为了澄清, WorkingOn:Employee_ID引用Employee.employee_ID,Project_ID引用Project.project_ID。
Employee-(1 to 1)-> WorkingOn-(1 to *)-> Project
答案 0 :(得分:0)
我建议仅在项目表上使用总和,如下所示:
CREATE OR REPLACE TRIGGER testtrigger
AFTER INSERT ON WorkingOn
FOR EACH ROW
BEGIN
UPDATE employee
SET TOTAL_BUDGET = ( SELECT SUM(budget)
FROM project
WHERE project.project_id = :NEW.project_id)
WHERE employee_id = :NEW.employee_id;
END;
/
干杯!