PL SQL 插入和更新后循环 Table

PL SQL Loop Table after insert and update

我正在尝试在插入到不同的 table 调用后循环遍历一个称为代理的 table 并更新代理上的值 table 这就是我所拥有的。当我尝试创建触发器时,它出现了一个错误 "Trigger created with compilation errors"

CREATE OR REPLACE TRIGGER broker_level_trigger
AFTER INSERT ON appointment

DECLARE
    counter integer := 1;

BEGIN
for o in (SELECT * FROM broker)
loop
    SELECT COUNT(appointment.broker_id) INTO app_number FROM appointment INNER JOIN broker ON broker.broker_id = appointment.broker_id WHERE broker.broker_id = counter;
    IF app_number > 15 THEN
    UPDATE broker SET broker_level = 'gold' WHERE broker_id = counter;
    counter := counter + 1;
end loop;

end;
/ 

经纪人 table 有一个名为 broker_level 的字段,它根据约会发生变化,我希望它在约会的 broker_id 字段超过 15 时发生变化

show err 真是一件美妙的事情。 (我创建了您使用的虚拟表)。

SQL> CREATE OR REPLACE TRIGGER broker_level_trigger
  2    AFTER INSERT ON appointment
  3  DECLARE
  4    counter integer := 1;
  5  BEGIN
  6    for o in (SELECT * FROM broker)
  7    loop
  8      SELECT COUNT(appointment.broker_id)
  9        INTO app_number
 10        FROM appointment INNER JOIN broker
 11          ON broker.broker_id = appointment.broker_id
 12        WHERE broker.broker_id = counter;
 13
 14      IF app_number > 15 THEN
 15         UPDATE broker SET
 16           broker_level = 'gold'
 17           WHERE broker_id = counter;
 18         counter := counter + 1;
 19
 20    end loop;
 21  end;
 22  /

Warning: Trigger created with compilation errors.

SQL> show err
Errors for TRIGGER BROKER_LEVEL_TRIGGER:

LINE/COL ERROR
-------- -----------------------------------------------------------------
18/7     PLS-00103: Encountered the symbol "LOOP" when expecting one of
         the following:
         if

如果你仔细观察,你会发现 IF 错过了它的 END IF。让我们添加它:

SQL> CREATE OR REPLACE TRIGGER broker_level_trigger
  2    AFTER INSERT ON appointment
  3  DECLARE
  4    counter integer := 1;
  5  BEGIN
  6    for o in (SELECT * FROM broker)
  7    loop
  8      SELECT COUNT(appointment.broker_id)
  9        INTO app_number
 10        FROM appointment INNER JOIN broker
 11          ON broker.broker_id = appointment.broker_id
 12        WHERE broker.broker_id = counter;
 13
 14      IF app_number > 15 THEN
 15         UPDATE broker SET
 16           broker_level = 'gold'
 17           WHERE broker_id = counter;
 18         counter := counter + 1;
 19      END IF;                          --> missing
 20    end loop;
 21  end;
 22  /

Warning: Trigger created with compilation errors.

SQL> show err
Errors for TRIGGER BROKER_LEVEL_TRIGGER:

LINE/COL ERROR
-------- -----------------------------------------------------------------
6/5      PL/SQL: SQL Statement ignored
7/12     PLS-00201: identifier 'APP_NUMBER' must be declared
8/7      PL/SQL: ORA-00904: : invalid identifier
12/5     PL/SQL: Statement ignored
12/8     PLS-00201: identifier 'APP_NUMBER' must be declared

APP_NUMBER现在不见了;您使用它,但从未声明过它。让我们现在开始吧:

SQL> CREATE OR REPLACE TRIGGER broker_level_trigger
  2    AFTER INSERT ON appointment
  3  DECLARE
  4    counter integer := 1;
  5    app_number number;                   --> missing
  6  BEGIN
  7    for o in (SELECT * FROM broker)
  8    loop
  9      SELECT COUNT(appointment.broker_id)
 10        INTO app_number
 11        FROM appointment INNER JOIN broker
 12          ON broker.broker_id = appointment.broker_id
 13        WHERE broker.broker_id = counter;
 14
 15      IF app_number > 15 THEN
 16         UPDATE broker SET
 17           broker_level = 'gold'
 18           WHERE broker_id = counter;
 19         counter := counter + 1;
 20      END IF;                          --> missing
 21    end loop;
 22  end;
 23  /

Trigger created.

SQL>

就是这样。


如果您不使用 SQL*Plus 而使用其他工具,您可以随时查询 user_errors:

Warning: Trigger created with compilation errors.

SQL> select line, position, text from user_errors where name = 'BROKER_LEVEL_TRIGGER' order by sequence;

 LINE  POSITION TEXT
----- --------- ------------------------------------------------------------
    8        12 PLS-00201: identifier 'APP_NUMBER' must be declared
    9         7 PL/SQL: ORA-00904: : invalid identifier
    7         5 PL/SQL: SQL Statement ignored
   13         8 PLS-00201: identifier 'APP_NUMBER' must be declared
   13         5 PL/SQL: Statement ignored

SQL>