-1
CREATE OR REPLACE TRIGGER TGR_UPT_CGPA
AFTER INSERT ON onlineAssessment
FOR EACH ROW
DECLARE
  v_totalCredit NUMBER(3):=0;
  v_learnerID VARCHAR(15);
  v_grade onlineAssessment.grade%TYPE;
  v_gradeNum NUMBER(2);

  CURSOR Grade_CURSOR IS
      SELECT COUNT(grade), grade
      FROM onlineAssessment
      WHERE learnerID = :new.learnerID GROUP BY grade;

BEGIN
  SELECT learnerID into v_learnerID
  FROM onlineAssessment
  WHERE AssessmentID = :new.AssessmentID;

  SELECT SUM(C.courseCredits) into v_totalCredit
  FROM Courses C, onlineAssessment A
  WHERE A.learnerID = :new.learnerID AND C.CourseID = A.CourseID;

  OPEN Grade_CURSOR;

  LOOP
    FETCH Grade_CURSOR INTO v_gradeNum, v_grade;
    EXIT WHEN Grade_CURSOR%NOTFOUND;
    IF v_grade='A' THEN
      v_totalCredit := v_totalCredit+(v_gradeNum*4);
    ELSIF v_grade='B' THEN
      v_totalCredit := v_totalCredit+(v_gradeNum*3);
    ELSIF v_grade='C' THEN
      v_totalCredit := v_totalCredit+(v_gradeNum*2);
    ELSIF v_grade='D' THEN
      v_totalCredit := v_totalCredit+(v_gradeNum*2);
    ELSE
      v_totalCredit := v_totalCredit+(v_gradeNum*0);
    END IF;
  END LOOP;
  CLOSE Grade_CURSOR;

  UPDATE learners
  SET CGPA = v_totalCredit
  WHERE learnerID = :new.learnerID;


END;
/

触发器创建没有问题,但是当我将新数据插入在线评估表时,它不会更新学习者的 CGPA 数据..我无法解决这个问题,请帮助我,谢谢..

我试图弄清楚几乎半天,我无法解决这个问题。我是 Oracle SQL 的新手。

4

1 回答 1

1

您的触发器中包含会导致“变异触发器”错误的代码。谷歌“ora-04091”查看详细信息(这里是一个链接)。基本上,您无法查询触发器中当前正在更新其上下文的表。在您的情况下,您在 onlineAssessment 上有一个触发器,但在代码中,您有以下选择:

 SELECT learnerID into v_learnerID
  FROM onlineAssessment
  WHERE AssessmentID = :new.AssessmentID;

您需要删除选择,并简单地引用:new.learnerID。您已经在下一个查询中引用了它:

 SELECT SUM(C.courseCredits) into v_totalCredit
  FROM Courses C, onlineAssessment A
  WHERE A.learnerID = :new.learnerID AND C.CourseID = A.CourseID;

所以我什至不确定第一次选择的目的是什么。只需将其删除,您应该就可以了。

于 2013-07-15T00:45:54.540 回答