一些语法错误 - 无法弄清楚在哪里

时间:2011-02-18 16:51:55

标签: sql oracle plsql ora-06550

这是我编写的脚本,它在EXCEPTION块上有奇怪的语法错误。如果我删除异常块,脚本编译正确。但是一旦我把它写回来它就会给我错误

Error(58,11): PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following:     ( begin case declare else elsif end exit for goto if loop mod    null pragma raise return select update while with    <an identifier> <a double-quoted delimited-identifier>    <a bind variable> << continue close current delete fetch lock    insert open rollback savepoint set sql execute commit forall    merge pipe purge 

这是脚本

LOOP
  BEGIN
    SAVEPOINT check_point;

    EXIT WHEN DBMS_SQL.FETCH_ROWS (cursor_handle) = 0;
    DBMS_SQL.COLUMN_VALUE (cursor_handle, 1,  cc , col_err, actual_len);
    DBMS_SQL.COLUMN_VALUE (cursor_handle, 2,  di, col_err, actual_len);

    IF INSTR (cc, '_') <> 0 THEN
      cc := Trim (cc);
      cc := Upper(cc);
      cc := substr(cc,4,2);

      EXECUTE IMMEDIATE 'UPDATE  ' || dest || ' SET cc = :v1 WHERE di = :v2' 
        USING cc, di;

      if SQL%ROWCOUNT > 0 THEN
        inserts := inserts + 1;
        counter := counter + 1;
        IF counter > 500 THEN
          counter := 0;
          COMMIT;
        END IF;
      END IF;

      EXCEPTION
        WHEN DUP_VAL_ON_INDEX THEN
          dups := dups+1;
          ROLLBACK TO check_point;
        WHEN VALUE_ERROR THEN
          valerr := valerr +1;
          ROLLBACK TO check_point;
        WHEN OTHERS THEN
          DBMS_OUTPUT.PUT_LINE('errno: ' || TO_CHAR(SQLCODE) || ' Msg: ' || SQLERRM);
          otherexc := otherexc +1;
        IF otherexc > 50 THEN 
          EXIT;
        END IF;
        ROLLBACK TO check_point;              
    END IF;
  END;
END LOOP;

我知道问这样一个问题很烦人,但我无法弄清楚那是什么错误。我是Pl / SQL的雷曼。

1 个答案:

答案 0 :(得分:3)

错误似乎是您的EXCEPTION子句在IF INSTR (cc, '_') <> 0 IF语句中,但您似乎希望将EXCEPTION与循环顶部的BEGIN语句匹配。我相信你想要移动END IF;对于EXCEPTION之前的IF INSTR (cc, '_') <> 0,就像我在这里一样

LOOP
  BEGIN
    SAVEPOINT check_point;

    EXIT WHEN DBMS_SQL.FETCH_ROWS (cursor_handle) = 0;
    DBMS_SQL.COLUMN_VALUE (cursor_handle, 1,  cc , col_err, actual_len);
    DBMS_SQL.COLUMN_VALUE (cursor_handle, 2,  di, col_err, actual_len);

    IF INSTR (cc, '_') <> 0 THEN
      cc := Trim (cc);
      cc := Upper(cc);
      cc := substr(cc,4,2);

      EXECUTE IMMEDIATE 'UPDATE  ' || dest || ' SET cc = :v1 WHERE di = :v2' 
        USING cc, di;

      if SQL%ROWCOUNT > 0 THEN
        inserts := inserts + 1;
        counter := counter + 1;
        IF counter > 500 THEN
          counter := 0;
          COMMIT;
        END IF; -- IF counter > 500
      END IF; -- IF SQL%ROWCOUNT > 0
    END IF; -- INSTR (cc, '_') <> 0


  EXCEPTION
    WHEN DUP_VAL_ON_INDEX THEN
      dups := dups+1;
      ROLLBACK TO check_point;
    WHEN VALUE_ERROR THEN
      valerr := valerr +1;
      ROLLBACK TO check_point;
    WHEN OTHERS THEN
      DBMS_OUTPUT.PUT_LINE('errno: ' || TO_CHAR(SQLCODE) || ' Msg: ' || SQLERRM);
      otherexc := otherexc +1;
      IF otherexc > 50 THEN 
        EXIT;
      END IF;
      ROLLBACK TO check_point;              
  END;
END LOOP;

然而,有人说,我可能会重写代码。每500行提交几乎肯定是一个错误。我非常怀疑你的WHEN OTHERS异常处理程序 - 我真的认为你至少要将错误写入表或填充错误集合而不是写入DBMS_OUTPUT缓冲区,这可能是也可能不是显示。

LOOP
  SAVEPOINT check_point;

  EXIT WHEN DBMS_SQL.FETCH_ROWS (cursor_handle) = 0;
  DBMS_SQL.COLUMN_VALUE (cursor_handle, 1,  cc , col_err, actual_len);
  DBMS_SQL.COLUMN_VALUE (cursor_handle, 2,  di, col_err, actual_len);

  IF INSTR (cc, '_') <> 0 THEN
    cc := Trim (cc);
    cc := Upper(cc);
    cc := substr(cc,4,2);

    BEGIN
      EXECUTE IMMEDIATE 'UPDATE  ' || dest || ' SET cc = :v1 WHERE di = :v2' 
        USING cc, di;

      if SQL%ROWCOUNT > 0 THEN
        inserts := inserts + 1;
      END IF; 
    EXCEPTION
      WHEN DUP_VAL_ON_INDEX THEN
        dups := dups+1;
        ROLLBACK TO check_point;
      WHEN VALUE_ERROR THEN
        valerr := valerr +1;
        ROLLBACK TO check_point;
      WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('errno: ' || TO_CHAR(SQLCODE) || ' Msg: ' || SQLERRM);
        otherexc := otherexc +1;
        IF otherexc > 50 THEN 
          EXIT;
        END IF;
        ROLLBACK TO check_point;              
    END;

  END IF; -- INSTR (cc, '_') <> 0
END LOOP;