SQL错误 - 游标格式错误或不完整

时间:2017-07-21 16:59:29

标签: cursor oracle-sqldeveloper malformed

我的目标是在循环中运行嵌套游标,以便显示部分名称和编号,然后显示该部分中的所有人员。

首先是代码。

SET SERVEROUTPUT ON SIZE 4000

DECLARE
  search VARCHAR2(20) := 'Summer 2007';
  CURSOR cur_class IS
    SELECT call_id, sec_num, c_sec_id
    FROM course_section
    INNER JOIN course ON course_section.course_id = course.course_id
    INNER JOIN term ON course_section.term_id = term.term_id
    WHERE term.term_desc = search
    ORDER BY course.call_id;
  f_cur cur_class%ROWTYPE;
  CURSOR cur_students IS
    SELECT c_sec_id, s_first, s_last
    FROM enrollment
    INNER JOIN student ON enrollment.s_id = student.s_id
    INNER JOIN course_section ON enrollment.c_sec_id = course_section.c_sec_id
    WHERE enrollment.c_sec_id IS NOT NULL;
  f_cur_stu cur_students%ROWTYPE;

BEGIN
  OPEN cur_class;
  LOOP
    FETCH cur_class INTO f_cur;
    EXIT WHEN cur_class%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE(f_cur.call_id || ' Sec. ' || f_cur.sec_num);
      FOR f_cur_stu IN cur_students LOOP
        IF f_cur.c_sec_id = f_cur_stu.c_sec_id THEN
          DBMS_OUTPUT.PUT_LINE(f_cur_stu.s_first || ' ' || f_cur_stu.s_last);
        END IF;
      END LOOP;
    END LOOP;
    CLOSE cur_class;
END;

以下是错误消息:

Error report:
ORA-06550: line 16, column 11:
PL/SQL: ORA-00918: column ambiguously defined
ORA-06550: line 12, column 5:
PL/SQL: SQL Statement ignored
ORA-06550: line 11, column 10:
PLS-00341: declaration of cursor 'CUR_STUDENTS' is incomplete or malformed
ORA-06550: line 17, column 13:
PL/SQL: Item ignored
ORA-06550: line 26, column 29:
PLS-00364: loop index variable 'F_CUR_STU' use is invalid
ORA-06550: line 26, column 9:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

如果我注释掉整个第二个光标创建和嵌套循环,第一个循环就会起作用,根据需要列出部分名称和编号。

当我尝试第二个游标cur_students时会出现问题。我不知道光标是如何格式错误/不完整的,以及我可以做些什么来完成它。我已经尝试了其他WHERE子句,它总是一样的。

编辑:表格结构的图片: Table Structure

2 个答案:

答案 0 :(得分:0)

通常的嫌疑人就好像您的表格enrollment没有名为c_sec_id的列。

您还可以共享表结构吗?

答案 1 :(得分:0)

1)SELECT c_sec_id, s_first, s_last

c_sec_id出现在enrollmentcourse_section表中。您必须在其前面添加表名称,即enrollment.c_sec_idcourse_section.c_sec_id,否则编译器不清楚您要从哪个表中检索它。

2)   f_cur_stu cur_students%ROWTYPE;

您不需要此声明,因为f_cur_stu中的FOR f_cur_stu IN cur_students LOOP是由游标循环本身隐式创建的变量。