使用受限选择 oracle 在创建游标后插入数据

Inserting data following cursor creation with restricted selection oracle

这是我第一次在 Stack Overflow 上发帖。我通常只是搜索以前的问题并找到我的答案,但在这种情况下,我似乎无法在这里或更广泛的互联网上找到我的问题的答案。

我遇到的问题是我想填充一个多对多 link table 用于自动存储考勤。当一个新学生被添加到一门课程时,学生的 StudentNo 和每个适用的 Lesson 的 LessonNo 将作为主键输入到 link table 中。另外,学生的考勤码会自动设置为'T',表示考勤待确认。

我不希望这是一个过程,因为数据库预计有超过 6,000 名活跃学生没有条目,每个学生都有一百多节课。只有家庭学生(他们的定义,而不是我的定义)上大学。 homeStudent 主键与整体学生主键相同。

我的触发器如下:

CREATE OR REPLACE TRIGGER Student_Added_to_Course
AFTER INSERT ON HomeStudent
FOR EACH ROW
DECLARE 
CURSOR applicable_lessons
IS
   Select Student.studentNo, Lesson.LessonNo
   FROM
   Student, Lesson, Course, Module
   WHERE
   Student.StudentNo = :new.studentNo
   AND
   Student.CourseNo = Course.CourseNo
   AND
   Course.CourseNo = Module.CourseNo
   AND
   Lesson.ModuleNo = Module.ModuleNo;
BEGIN
 FOR I IN applicable_lessons
 LOOP
 INSERT INTO Attendance (StudentNo, LessonNo, AttendanceCode)
 VALUES (I.student.studentNo, I.Lesson.LessonNo, 'T');
  END LOOP;
END;
/

我收到以下错误:

第 18 行:SQL 语句被忽略 第 19 行:必须声明组件 'Lesson' 第 19 行:此处不允许使用列

因为我的所有错误都是从第 18 行开始发生的,所以我假设我在插入循环中犯了错误?

谁能告诉我我做错了什么。我认为修复相当简单,但我有点斜眼看着我自己的工作,找不到错误。

提前谢谢你, 博布雷克

如果将 INSERT 语句更改为

INSERT INTO Attendance (StudentNo, LessonNo, AttendanceCode)
VALUES (I.studentNo, I.LessonNo, 'T');

你应该很好。

因为游标变量是i并且游标选择了studentnolessonno列;它们属于哪个 table 并不重要(不再)- 您可以通过游标变量的名称引用它们,即 i.studentnoi.lesonno.

既然可以只执行一个插入语句,为什么还要遍历游标?这样会更快!

例如:

CREATE OR REPLACE TRIGGER Student_Added_to_Course
AFTER INSERT ON HomeStudent
FOR EACH ROW
BEGIN

  INSERT INTO attendance (studentno, lessonno, attendancecode)
  SELECT s.studentno,
         l.lessonno,
         'T'
  FROM   student s
         INNER JOIN course c ON s.courseno = c.courseno
         INNER JOIN module m ON c.courseno = m.courseno
         INNER JOIN lesson l ON m.moduleno = l.moduleno
  WHERE  s.studentno = :new.studentno;

END Student_Added_to_Course;
/

请注意,我已将您的连接语法更改为使用 ANSI 连接语法,请注意如何更容易查看表的链接方式。