我正在使用PostgreSQL。我需要创建一个触发器来保持学生的GPA是最新的。我声明了r变量来遍历SQL语句。但是,我不能使用其他表格的字段而不是像r.credits, r.grade
这样的学生。我应该如何声明这种类型的变量?
CREATE OR REPLACE FUNCTION updateGPA() RETURNS TRIGGER AS $$
DECLARE
r Student;
total_credits INT;
temp INT;
BEGIN
FOR r IN (SELECT s.sid, c.credits, t.grade FROM Student s, Course c, Take t WHERE t.cid = NEW.cid and t.sid = s.sid)
LOOP
total_credits = SUM(r.credits);
temp = temp + SUM(r.credits * r.grade);
UPDATE Student SET gpa = temp/total_credits WHERE Student.sid = r.sid;
END LOOP;
RETURN NEW;
END;