触发帮助 - 无法触发

时间:2013-07-14 18:20:11

标签: sql oracle triggers cursor

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的新手。

1 个答案:

答案 0 :(得分:1)

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

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

您需要删除select,并简单地引用:new.learnerID。您已在下一个查询中引用该内容:

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

所以我甚至不确定第一选择的目的是什么。只需删除它,你就可以了。