通过触发器在第一个 table 中插入行时在另一个 table 中插入行?

Insert row in another table when insert in first table by trigger?

每当我在 stud_master table 中插入行时,我都试图在 stud_report table 中每次插入一行。为此,我尝试在触发器的帮助下遵循 SQL 代码,但我得到:

Warning: Trigger created with compilation errors.

CREATE OR REPLACE TRIGGER trg
      AFTER INSERT ON stud_master
     FOR EACH ROW
    DECLARE
      roll number;
      sum number;
      per decimal(5,2);
   BEGIN
        roll := stud_master.roll_no;
        sum :=  stud_master.sub1+stud_master.sub2+stud_master.sub3;
        per := (0.33*(sum));
        insert into stud_report values (roll,sum,per);
   END;
   /

Stud_master table 包含

+---------+--------------+----------+
| Field   | Type         | Null?    |
+---------+--------------+----------+
| roll_no | number       | NOT NULL |
| name    | varchar2(15) |          |
| sub1    | number       |          |
| sub2    | number       |          |
| sub3    | number       |          |
| Address | varchar2(30) |          |    
+---------+--------------+----------+

Stud_report table 包含

+------------+--------------+----------+
| Field      | Type         | Null?    |
+------------+--------------+----------+
| roll_no    | number       | NOT NULL |
| total      | number       |          |
| percentage | decimal(5,2) |          |
+------------+--------------+----------+

编译错误为:

8/26 PLS-00103: Encountered the symbol ")" when expecting one of the following:
(
9/49 PLS-00103: Encountered the symbol "," when expecting one of the following:
(
10/7 PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
;

你离得不远 - 使用 :new 代替 table 名称(我会避免使用 sum 作为变量名称)

create or replace 
TRIGGER trg
      AFTER INSERT 
      ON STUD_MASTER
     FOR EACH ROW

     DECLARE roll NUMBER;
       sumNO NUMBER;
       per decimal(5,2);
    BEGIN
        roll :=  :new.roll_no;
        sumNO:=  :new.sub1+:new.sub2+:new.sub3;
        per  := (0.33*(sumNO));
        --dbms_output.put_line('New roll no '|| roll || ' sum = ' || sumno || ' PER = ' || per);
        insert into stud_report  values (roll,sumNO,per);
   END ;
   /