0
ALTER TABLE student ADD gpa NUMBER;
CREATE OR REPLACE TRIGGER "USER36401"."GETGPA" AFTER
  DELETE OR
  INSERT OR
  UPDATE ON grade_report FOR EACH row DECLARE totalqp NUMBER :=0;

  totalgpa      NUMBER :=0;
  totalcreds    NUMBER :=0;
  prevqp        NUMBER :=0;
  prevgpa       NUMBER :=0;
  prevcreds     NUMBER :=0;
  incomingqp    NUMBER :=0;
  incominggpa   NUMBER :=0;
  incomingcreds NUMBER :=0;
  temp          NUMBER :=0;
  BEGIN
    CASE
    WHEN updating THEN
      UPDATE student SET student.gpa = NVL(student.gpa,0);
      SELECT student.gpa
      INTO temp
      FROM student
      WHERE student.student_number = :NEW.student_Number;
      IF temp                      <1 THEN
        totalqp                   := temp;
        SELECT DISTINCT DECODE(:NEW.GRADE,'A','4','B','3','C','2','D','1','F','0')
        INTO temp
        FROM student,
          section,
          course
        WHERE :NEW.student_number   = student.student_number
        AND :NEW.section_identifier = section.section_identifier
        AND section.course_number   = course.course_number;
        UPDATE student
        SET student.gpa              = temp
        WHERE student.student_number = :NEW.student_number;
      ELSE
        prevgpa := temp;
        SELECT student.total_credit_hours
        INTO temp
        FROM student
        WHERE student.student_number = :NEW.student_number;

        prevcreds := temp;
        SELECT DISTINCT course.credit_hours
        INTO temp
        FROM course,
          section
        WHERE course.course_number    = section.course_number
        AND section.section_identifier= :NEW.section_identifier;
        --current
        SELECT DISTINCT course.credit_hours
        INTO temp
        FROM course,
          section
        WHERE course.course_number    = section.course_number
        AND section.section_identifier= :NEW.section_identifier;

        incomingcreds := temp;
        prevcreds     := prevcreds - incomingcreds;
        prevqp        := prevgpa   * prevcreds;
        --total quality points before add
        SELECT DECODE(:NEW.GRADE,'A','4','B','3','C','2','D','1','F','0')
        INTO temp
        FROM student,
          section,
          course
        WHERE :NEW.student_number   = student.student_number
        AND :NEW.section_identifier = section.section_identifier
        AND section.course_number   = course.course_number;

        incominggpa := temp;
        --gpa being added before quality points
        SELECT DISTINCT course.credit_hours
        INTO temp
        FROM course,
          section
        WHERE course.course_number    = section.course_number
        AND section.section_identifier= :NEW.section_identifier;

        incomingqp := incominggpa*incomingcreds;
        totalqp    := prevqp     + incomingqp;
        totalcreds := prevcreds  + incomingcreds;
        totalgpa   := totalqp    / totalcreds;
        UPDATE student
        SET gpa                      = totalgpa
        WHERE student.student_number = :NEW.student_number;
      END IF;
    WHEN DELETING then
    Select * from student where student.student_number = :NEW.student_number;
    END CASE;
  END;

目标是让触发器在删除时执行“[尚未实现]”的操作。

当我编译时,它说编译,但有错误警告。决赛周我已经筋疲力尽了,这是最后一个项目。我找不到错误所在,我确定这是我没有发现的语法问题。任何帮助是极大的赞赏。

编辑:另外,我知道这可能是实现我正在尝试做的最无效的方式。但我的代码完全基于教授列出的我们可以在课堂上的幻灯片中使用的内容。坦率地说,这是针对我的次要专业,而且我是一名大四学生,所以我并不在乎。我只需要它工作。

编辑3:

从编译触发脚本:ALTER TABLE student 成功。警告:执行已完成,警告 TRIGGER "USER36401"."GETGPA" 已编译。

insert into grade_report values ('17','112','B');
select * from student;
insert into grade_report values ('17','119','C');
select * from student;
insert into grade_report values ('8','85','A');
select * from student;
insert into grade_report values ('8','92','A');
select * from student;
insert into grade_report values ('8','102','B');
select * from student;
insert into grade_report values ('8','135','A');

返回错误:“从命令中的第 1 行开始的错误:插入到grade_report 值('17','112','B')命令行错误:1 列:12 错误报告:SQL 错误:ORA-04098:触发器' USER36401.GETGPA' 无效并且重新验证失败 04098. 00000 - “触发器 '%s.%s' 无效并且重新验证失败” *原因:尝试检索触发器以执行并且发现无效. 这也意味着触发器的编译/授权失败。*操作:选项是解决编译/授权错误、禁用触发器或删除触发器。"

4

1 回答 1

0

您最后一个选择语句(在案例的“删除”分支中),没有“INTO”子句。此外,您使用SELECT *, 而您需要指定要选择到变量中的确切字段。

您在案件的另一个分支中做同样的事情,为什么不看看呢?

于 2012-12-08T09:38:45.593 回答