Search code examples
oracle-databasetriggerssubstringcursors

How in implement Triggers in Oracle


Using Oracle 11g, I am trying to get the last character of a the name of a course via a trigger and then depending on the last character, It assigns a grade to the student.

I have tried several different approaches to the problem, I get several different errors, its giving me a headache. I am not sure if I am approaching the cursor wrong or the whole problem.

INSERT INTO GRADES (STUDNETID, SECTIONID, SECTIONINFO, COURSEID,COURSENAME, GRADE) VALUES (234501,153, '078',256877 , 'Intro to Holographic',"") ;
INSERT INTO GRADES (STUDNETID, SECTIONID, SECTIONINFO, COURSEID,COURSENAME, GRADE) VALUES (234519,153, '078',256877 ,'Intro to Holographic' ,"") ;
INSERT INTO GRADES (STUDNETID, SECTIONID, SECTIONINFO, COURSEID,COURSENAME ,GRADE) VALUES (234505,153, '078',256877 ,'Intro to Holographic', "") ;
INSERT INTO GRADES (STUDNETID, SECTIONID, SECTIONINFO, COURSEID,COURSENAME, GRADE) VALUES (23405,21, '012A',23532 ,'Starfleet Intro', "") ;
INSERT INTO GRADES (STUDNETID, SECTIONID, SECTIONINFO, COURSEID,COURSENAME, GRADE) VALUES (23419,21, '012A',23532 ,'Starfleet Intro', "") ;
INSERT INTO GRADES (STUDNETID, SECTIONID, SECTIONINFO, COURSEID,COURSENAME, GRADE) VALUES (23401,21, '012A',23532 ,'Starfleet Intro', "") ;
INSERT INTO GRADES (STUDNETID, SECTIONID, SECTIONINFO, COURSEID,COURSENAME, GRADE) VALUES (23418,21, '012A',23532 ,'Starfleet Intro', "") ;
INSERT INTO GRADES (STUDNETID, SECTIONID, SECTIONINFO, COURSEID,COURSENAME, GRADE) VALUES (234520,933,'278',843234 ,'Borg Technolgy', "");
INSERT INTO GRADES (STUDNETID, SECTIONID, SECTIONINFO, COURSEID,COURSENAME, GRADE) VALUES (234505,933,'278',843234 ,'Borg Technolgy' ,"");
INSERT INTO GRADES (STUDNETID, SECTIONID, SECTIONINFO, COURSEID,COURSENAME, GRADE) VALUES (234501,933,'278',843234 ,'Borg Technolgy' ,""); 
INSERT INTO GRADES (STUDNETID, SECTIONID, SECTIONINFO, COURSEID, COURSENAME,GRADE) VALUES (234519,438,'531',542303 , 'Intro to Genetics',""); 

create or replace TRIGGER gradeTrigger
AFTER Update ON Grade For Each Row
  declare 

 cursor courseTitle_cur IS 
  select g.COURSENAME
  from Grade g;

lastChar String (1);
gradeLetter String(1); 
   BEGIN

   for courseTitle_cur in CourseName
                 loop
        SubStr(CourseName, -1):= lastChar;
      end loop;
   If(lastChar = 'A' OR lastChar ='B'OR lastChar = 'C' OR lastChar = 'D' OR lastChar = 'E'OR lastChar = 'F') THEN
 gradeLetter := "A";

ELSIF(lastChar = 'G'OR lastChar = 'H'OR lastChar = 'I'OR lastChar = 'J'OR lastChar = 'K')THEN
 gradeLetter := "B";

ELSIF (LASTCHAR = 'L'or LASTCHAR = 'M'or LASTCHAR = 'N'or LASTCHAR = 'O'or LASTCHAR = 'P')then
 gradeLetter := "C";

ELSIf (LASTCHAR = 'Q' OR LASTCHAR = 'R'OR LASTCHAR = 'S'OR LASTCHAR = 'T')THEN
 gradeLetter := "D";

ELSIf (lastChar = 'U' OR lastChar ='V'OR lastChar = 'W'OR lastChar = 'X'OR lastChar = 'Y' OR lastChar = 'Z')THEN
 gradeLetter := "E";

Return gradeLetter;
end if ; 
   End;

I get errors like 'Error(16,2): PL/SQL: Statement ignored' or 'Error(4,10): PL/SQL: ORA-00904: "G"."COURSENAME": invalid identifier' This is very frustrating because I cannot pinpoint why its being ignored or is invalid.


Solution

  • I think you have misunderstood how triggers work in Oracle.

    Each row that is updated has a before and after state - these are represented by the :old and :new records respectively.

    If you want to specify what the value of a field should look like after the update is applied, you need to change the field in the :new record before the update is applied.

    Therefore, I think your trigger should probably look something like:

    CREATE OR REPLACE TRIGGER grade_trigger
      BEFORE INSERT OR UPDATE ON grades
      FOR EACH ROW
    BEGIN
      IF substr(upper(:new.coursename), -1) IN ('A', 'B', 'C', 'D', 'E', 'F')
      THEN
        :new.grade := 'A';
      ELSIF substr(upper(:new.coursename), -1) IN ('G', 'H', 'I', 'J', 'K')
      THEN
        :new.grade := 'B';
      ELSIF substr(upper(:new.coursename), -1) IN ('L', 'M', 'N', 'O', 'P')
      THEN
        :new.grade := 'C';
      ELSIF substr(upper(:new.coursename), -1) IN ('Q', 'R', 'S', 'T')
      THEN
        :new.grade := 'D';
      ELSIF substr(upper(:new.coursename), -1) IN ('U', 'V', 'W', 'X', 'Y', 'Z')
      THEN
        :new.grade := 'E';
      END IF;
    END grade_trigger;
    /
    

    and here's proof that it works.