奇怪的PL SQL无效游标

时间:2013-05-03 11:20:00

标签: oracle stored-procedures plsql cursor

当运行以下代码时,我得到一个ORA-01001:无效游标不确定发生了什么,我将它转换为程序并且它开始发生;

CREATE OR REPLACE PROCEDURE p_student_from_class_list (p_stu_id IN NUMBER) 
IS
--Declaring a variable --
    v_date date; 

    --Creating a Cursor to find data from using the p_stu_id paramenter--
    CURSOR enrollments_cursor IS
        SELECT enrollment_date, stu_id, class_id, status
        FROM enrollments 
        WHERE stu_id = p_stu_id;


BEGIN
    /*Changing the date so the code looks for the classes for the student 
    which was entered in the bind variable above for the last 10 years*/
    v_date := add_months(SYSDATE, -120);
    FOR v_enrollment_record IN enrollments_cursor
    LOOP 
--Displays the student's enrollment date, class ID and Current Status for each    class >they taken in last 10 years,from the value which was entered in the bind    variable--
        IF v_enrollment_record.enrollment_date 
        between v_date AND SYSDATE THEN
            DBMS_OUTPUT.PUT_LINE('Enrollment Date: '
            ||v_enrollment_record.enrollment_date
            ||' Class ID: '||v_enrollment_record.class_id
            ||' Status: '||v_enrollment_record.status);
        END IF;
    END LOOP;
    --Closing the cursor --
    CLOSE enrollments_cursor;

    --Application Express processes the statement--
    COMMIT; 
    --Telling Application Express the procedure has finished--
END p_student_from_class_list;

  --Anonymous Block to run the Procedure--

BEGIN
    p_student_from_class_list(--Student ID--);
END;

正如我所说的那样,代码在程序中得到了工作但由于某种原因创建它作为一个程序现在给出了这个错误。我一直绞尽脑汁试图解决这个问题。

1 个答案:

答案 0 :(得分:2)

对于用作CLOSE的光标,您不需要手动FOR <record> IN <cursor> LOOP。您只需手动CLOSE光标OPEN FETCH ed(然后 CLOSE enrollments_cursor; 编辑)。

只需删除该行:

{{1}}

比较FOR LOOPOPEN-FETCH-CLOSE pattern的文档。