【问题标题】:when no_data_found stop the current iteration and resume the next当 no_data_found 停止当前迭代并恢复下一个
【发布时间】:2013-01-09 04:56:45
【问题描述】:

我有如下代码:

DECLARE
rec emp%ROWTYPE
BEGIN
<<start_again>>
FOR rec IN c1 LOOP
    SELECT emp_index
    INTO   flag
    FROM   m_sme006
    WHERE  emp_id = rec.emp_id
           AND eff_dt = rec.eff_dt
           AND end_dt = rec.end_dt
           AND last_maint_ts > rec.last_maint_ts;
END LOOP;
EXCEPTION
    WHEN no_data_found THEN
      GOTO start_again;
END; 

每次出现 no_data_found 异常时,我都希望执行返回并继续下一次迭代。但它抛出了error PLS-00375: illegal GOTO statement

我还可以在异常块中包含这行dbms_output.Put_line('Outside exception block'rec.sme006_index); 以查看发生的位置吗?

提前致谢

【问题讨论】:

标签: oracle plsql


【解决方案1】:

您不能将程序流从异常块返回到执行块。您需要将异常块放入循环中,如下所示:

DECLARE
   rec emp%ROWTYPE
BEGIN
FOR rec IN c1 LOOP
    BEGIN
       SELECT emp_index
       INTO   flag
       FROM   m_sme006
       WHERE  emp_id = rec.emp_id
              AND eff_dt = rec.eff_dt
              AND end_dt = rec.end_dt
              AND last_maint_ts > rec.last_maint_ts;
    EXCEPTION
       WHEN no_data_found THEN
          dbms_output.Put_line('Outside exception block ' || rec.sme006_index);
    END;
END LOOP;
END;

【讨论】:

    【解决方案2】:

    我相信您正在寻找这样的东西:

    DECLARE
      rec emp%ROWTYPE
    BEGIN
      FOR rec IN c1 LOOP
        BEGIN
            SELECT emp_index
            INTO   flag
            FROM   m_sme006
            WHERE  emp_id = rec.emp_id
               AND eff_dt = rec.eff_dt
               AND end_dt = rec.end_dt
               AND last_maint_ts > rec.last_maint_ts;
        EXCEPTION
            WHEN no_data_found THEN
              -- whatever code you want to log your error
        END;
      END LOOP;
    END; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 2012-10-15
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      相关资源
      最近更新 更多