Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

plsql - How do I properly deal with triggers in 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

When I run the above code, I get the following error

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')

Could anybody help me out here? I'm relatively new to triggers. My idea is to simply invoke this trigger whenever I insert a new record into student_det record and depending on the conditions given, display the grade obtained by the student.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Some syntax errors:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...