2013-05-03 57 views
0

当运行下面的代码时,我得到一个ORA-01001:无效光标不知道发生了什么,我将它转换为一个过程并开始发生;Wierd PL SQL无效光标

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; 

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

回答

2

对于您用作FOR <record> IN <cursor> LOOP的游标,您不需要手册CLOSE。你只需要CLOSE一个你手动的光标OPEN ed(然后FETCH ed)。

只是删除行:

CLOSE enrollments_cursor; 

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

+0

哇完全没有看到!非常感谢你,你的救星= D – Phyore 2013-05-03 11:29:04

+0

没问题。顺便提一句,您可以将'v_date'作为[光标参数](http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/static.htm#CIHFAHFE)并将其用于光标的' WHERE条款;那么你不需要循环内的IF。 – 2013-05-03 11:34:55

+0

谢谢你的信息:)由于被要求输入他们的问题,这些问题被设置在问题中。不幸的是,日期并没有被告知输入〜 – Phyore 2013-05-03 15:15:31