1

我面临一个奇怪的问题。它向我展示了:

第 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;

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

4

2 回答 2

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;
于 2013-07-26T18:56:17.137 回答
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;

分享和享受。

于 2013-07-26T21:02:27.040 回答