简体   繁体   English

PL / SQL触发器更新或删除

[英]PL/SQL Trigger Update or Delete

I have two table where their output looks like: 我有两个表,它们的输出看起来像:

Project Table
CODE         NAME                           
------------ -------------- 
101          Alpha                       
222          Beta                       
355          Gamma                       
973          Delta                   

Assignment Table
ID    NAME         PROJCODE       HOURS              
----- ------------ -------------- ------------ 
55055 Smith        101            20                     
55055 Smith        222            10                     
39002 Hammond      973            25                     
00001 Preston      355            5                      
10000 Logan        355            5                      
00777 Bond         222            20                        

If the last person from a project in the Assignment Table is updated/deleted (so then the projcode has no person assigned to it), I want to remove it from the Project Table. 如果工作分配表中项目的最后一个人被更新/删除(因此项目代码中没有人分配),我想将其从项目表中删除。 And I want to output it to DBMS_OUTPUT. 我想将其输出到DBMS_OUTPUT。 I wrote the following trigger but am getting an error each time I try to test the trigger. 我编写了以下触发器,但是每次尝试测试触发器时都会出错。

CREATE OR REPLACE TRIGGER project_removal_trigger AFTER
  DELETE OR
  UPDATE ON ASSIGNMENT FOR EACH ROW DECLARE PRINT project.code%type;
  BEGIN
    DELETE
    FROM PROJECT
    WHERE code NOT IN
      (SELECT Projcode FROM assignment GROUP BY projcode HAVING COUNT(name) > 0
      );
  END;

What am I doing wrong? 我究竟做错了什么?

Trigger can't select from a table it's fired for. 触发器无法从为其触发的表中选择。 Otherwise you'll get ORA-04091: table XXXX is mutating, trigger/function may not see it. 否则,您会收到ORA-04091:表XXXX正在突变,触发器/函数可能看不到它。 try not to put too much logic into triggers. 尽量不要在触发器中添加过多的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM