更新期间PL/SQL编译错误

Posted

技术标签:

【中文标题】更新期间PL/SQL编译错误【英文标题】:PL/SQL compilation error during update 【发布时间】:2012-01-03 19:28:52 【问题描述】:

查询

DECLARE
  v_vendor_id NUMBER;
BEGIN
  FOR vp_rec IN ( SELECT * FROM vpprocess )
    LOOP
      SELECT i.vendor_id INTO v_vendor_id FROM item i, lineitems l WHERE l.itemid = i.itemid AND l.id = vp_rec.lineitemid;
        EXCEPTION
            WHEN no_data_found THEN SELECT 0 INTO v_vendor_id FROM dual;
      UPDATE vpprocess SET vendorid = v_vendor_id WHERE vpprocessid = vp_rec.vpprocessid;
    END;
  END LOOP;
END;

错误信息

 Error starting at line 1 in command:
DECLARE
  v_vendor_id NUMBER;
BEGIN
  FOR vp_rec IN ( SELECT * FROM vpprocess )
    LOOP
      SELECT i.vendor_id INTO v_vendor_id FROM item i, lineitems l WHERE l.itemid = i.itemid AND l.id = vp_rec.lineitemid;
        EXCEPTION
            WHEN no_data_found THEN SELECT 0 INTO v_vendor_id FROM dual;
      UPDATE vpprocess SET vendorid = v_vendor_id WHERE vpprocessid = vp_rec.vpprocessid;
    END;
  END LOOP;
END;
Error report:
ORA-06550: line 7, column 9:
PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following:

   begin case declare 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> <<
   close current delete fetch lock insert open rollback
   savepoint set sql execute commit forall merge pipe
ORA-06550: line 11, column 7:
PLS-00103: Encountered the symbol "LOOP" when expecting one of the following:

   ;
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

【问题讨论】:

【参考方案1】:

您在 FOR 循环中使用了错误的语法。错误消息会告诉您究竟出了什么问题。而不是 BEGIN PUT LOOP 这个词。

作为个人规则,我从不使用BEGIN IN PL/SQL,只有很少的异常,例如,如果我只需要在某个代码块上捕获异常,并且该代码还不是循环或函数身体什么的。

根据 OP 的编辑进行编辑:这似乎是其中一种情况。请尝试以下操作:

DECLARE
  v_vendor_id NUMBER;
BEGIN
  FOR vp_rec IN ( SELECT * FROM vpprocess )
    LOOP
     BEGIN
       SELECT i.vendor_id INTO v_vendor_id FROM item i, lineitems l WHERE l.itemid = i.itemid AND l.id = vp_rec.lineitemid;
        EXCEPTION
            WHEN no_data_found THEN v_vendor_id := 0;
      END;  
      UPDATE vpprocess SET vendorid = v_vendor_id WHERE vpprocessid = vp_rec.vpprocessid;
    /* END; has been removed */
    END LOOP;
END;

【讨论】:

Dan:我确实尝试过替换 LOOP,但没有帮助。我正在更新错误和查询。 好的,我更新了我的答案。我面前不再有 Oracle,所以这是一个猜测。但基本上EXCEPTION WHEN 是一个块范围功能,因此您需要围绕您想要捕获异常的那些语句(在您的情况下为单个语句)创建一个内部块。

以上是关于更新期间PL/SQL编译错误的主要内容,如果未能解决你的问题,请参考以下文章

PL/SQL 过程编译错误

声明变量的 PL/SQL 编译错误

PL/SQL 编译错误

PL/SQL 编译失败,没有错误消息

PL/SQL 包编译错误:PLS-00323

PL/SQL:使用编译错误创建的函数 [关闭]