Oracle:遇到符号“文件结束”

时间:2013-07-26 18:03:24

标签: oracle plsql oracle10g eof

我面临一个奇怪的问题。它告诉我:

  

第28行的错误:PLS-00103:遇到以下其中一项时遇到符号“文件结束”:

代码如下:

CREATE OR REPLACE FUNCTION st_atten_up(stu_id IN student_info.id%type,app_mon IN student_attendence.month%type)
RETURN NUMBER
    IS
        PRAGMA AUTONOMOUS_TRANSACTION;
        att1 NUMBER; 
        CURSOR c1(sid IN student_info.id%type, pmon IN student_attendence.month%type) IS
        SELECT ATTENDANCE
        FROM STUDENT_ATTENDENCE
        WHERE id = sid and month = pmon;

    -- You may want a smaller variable if you know the result will be smaller
    BEGIN
        open c1;
        fetch c1(stu_id,app_mon) into att1;
        c1;

        att1 :=att1 + 1;
        UPDATE STUDENT_ATTENDENCE
        SET STUDENT_ATTENDENCE.ATTENDANCE = att1
        where STUDENT_ATTENDENCE.ID = stu_id and STUDENT_ATTENDENCE.month = app_mon;
        COMMIT;
        return att1;

        EXCEPTION
        WHEN OTHERS THEN
            ROLLBACK;
            raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
    END;

我不明白这里有什么问题。

2 个答案:

答案 0 :(得分:1)

你可以在代码中间删除C1格式并检查我在下面的代码中做了同样的事情,请检查并告诉我。

BEGIN
    open c1;
    fetch c1(stu_id,app_mon) into att1;
    --c1; remove it and try

    att1 :=att1 + 1;
    UPDATE STUDENT_ATTENDENCE
    SET STUDENT_ATTENDENCE.ATTENDANCE = att1
    where STUDENT_ATTENDENCE.ID = stu_id and STUDENT_ATTENDENCE.month = app_mon;
    COMMIT;
    return att1;

    EXCEPTION
    WHEN OTHERS THEN
        ROLLBACK;
        raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END;

答案 1 :(得分:0)

我认为我们已达成共识,即问题是读取c1;的行 - 应该是close c1;。我将建议一个略有不同的解决方案 - 即使用游标FOR循环而不是独立游标,并在单次迭代后退出循环。这完全消除了检查没有提取数据的需要(原始版本没有完成),IMO使代码更容易阅读和理解:

CREATE OR REPLACE FUNCTION st_atten_up(stu_id IN student_info.id%type,
                                       app_mon IN student_attendence.month%type)
  RETURN NUMBER
IS
  PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
  FOR aRow IN (SELECT ID, MONTH, ATTENDANCE
                 FROM STUDENT_ATTENDENCE
                 WHERE ID = stu_id AND
                       MONTH = app_mon)
  LOOP
    aRow.ATTENDANCE := aRow.ATTENDANCE + 1;

    UPDATE STUDENT_ATTENDENCE
      SET ATTENDANCE = aRow.ATTENDANCE
      WHERE ID = aRow.ID and
            MONTH = aRow.MONTH;
    COMMIT;

    RETURN aRow.STUDENT_ATTENDANCE;
  END LOOP;

  RETURN NULL;  -- no data found in FOR loop
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Exception: ' || SQLCODE || '  ' || SQLERRM);
    ROLLBACK;
    raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END ST_ATTEN_UP;

分享并享受。