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

发布于 2024-10-18 00:17:31 字数 1792 浏览 2 评论 0原文

这是我编写的脚本,它在 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 的雷曼。

Here is the script that i wrote and it has weird syntax error at EXCEPTION block. If i remove exception block the script compiles properly. but no sooner i write it back it gives me error

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 

Here is the script

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;

I know its very annoying to ask such kind a question but i am unable to figure out what error is that. I am lehman at Pl/SQL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救赎№ 2024-10-25 00:17:31

该错误似乎是您的 EXCEPTION 子句位于 IF INSTR (cc, '_') <> 内。 0 IF 语句,但您似乎希望将 EXCEPTION 与循环顶部的 BEGIN 语句相匹配。我相信你想移动 END IF;对于 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;

The error appears to be that your EXCEPTION clause is inside the IF INSTR (cc, '_') <> 0 IF statements but you appear to want to match the EXCEPTION to the BEGIN statement at the top of your loop. I believe that you want to move the END IF; for the IF INSTR (cc, '_') <> 0 before the EXCEPTION as I do here

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;

That being said, however, I would probably rewrite the code a bit. Committing every 500 rows is almost certainly an error. I'm very dubious of your WHEN OTHERS exception handler-- I would really think that you'd want to at least write the error to a table or populate a collection of errors rather than writing to the DBMS_OUTPUT buffer that may or may not ever be displayed.

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文