插入和更新后的PL SQL循环表

时间:2020-03-26 15:45:41

标签: sql oracle plsql sqlplus database-trigger

我正在尝试在称为“约会”的其他表中插入一个表后,遍历一个名为Broker的表,并更新我拥有的Broker表上的值。当我尝试创建触发器时,它会显示一条错误消息,“触发了编译错误”

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;
/ 

broker表中有一个名为broker_level的字段,它会根据约会进行更改,如果约会上的broker_id字段超过15个,我希望它进行更改

1 个答案:

答案 0 :(得分:2)

/*Add Product*/ app.post('/save', function (req, res) { console.log(req.body); for(let i=0; i<req.body.product; i++) { let sql = "insert into products values(null, '" + req.body.product + "')"; res.status(204).send(); connection.query(sql,[i], function (err) { if (err) throw err; }); connection.end(); } }); 是一件很棒的事情。 (我创建了您使用的虚拟表)。

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 * Plus,而是使用其他工具,则始终可以查询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>

user_errors