首页 > 解决方案 > 如何正确处理 PL SQL 中的触发器?

问题描述

create or replace trigger grade_
before insert on student_det
for each row
BEGIN
CASE 
WHEN new.grade > 10 then dbms_output.put_line('A');
WHEN new.grade > 20 then dbms_output.put_line('A+');
ELSE dbms_output.put_line('Failed');
END
END

当我运行上面的代码时,我收到以下错误

Error at line 4: PLS-00103: Encountered the symbol "WHEN" when expecting one of the following:

   := . ( % ;
The symbol ";" was substituted for "WHEN" to continue.

Error at line 6: PLS-00103: Encountered the symbol "END" when expecting one of the following:

   := . ( % ;

Error at line 5: PLS-00103: Encountered the symbol "ELSE" when expecting one of the following:

   := . ( % ;
The symbol ";" was substituted for "ELSE" to continue.



2. before insert on student_det
3. for each row
4. BEGIN
5. CASE new.grade
6. WHEN 10 then dbms_output.put_line('A')

有人可以帮我吗?我对触发器比较陌生。我的想法是每当我将新记录插入 student_det 记录时简单地调用此触发器,并根据给定的条件显示学生获得的成绩。

标签: plsqltriggers

解决方案


一些语法错误:

CREATE OR REPLACE TRIGGER grade_
   BEFORE INSERT
   ON student_det
   FOR EACH ROW
BEGIN
   CASE
      WHEN :new.grade > 10              /* :new instead of new */
      THEN
         DBMS_OUTPUT.put_line ('A');
      WHEN :new.grade > 20              /* :new instead of new */
      THEN
         DBMS_OUTPUT.put_line ('A+');
      ELSE
         DBMS_OUTPUT.put_line ('Failed');
   END CASE;                            /* missing CASE and semicolon */
END;                                    /* missing semicolon */

推荐阅读