Oracle 10g PL/SQL 中的“CONTINUE”关键字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/177752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:32:27  来源:igfitidea点击:

'CONTINUE' keyword in Oracle 10g PL/SQL

oraclecontinue

提问by Christopher McAtackney

I'm migrating a TSQL stored procedure to PL/SQL and have encountered a problem - the lack of a CONTINUE keyword in Oracle 10g.

我正在将 TSQL 存储过程迁移到 PL/SQL,但遇到了一个问题 - Oracle 10g 中缺少 CONTINUE 关键字。

I've read that Oracle 11g has this as a new feature, but upgrading is not an option unfortunately.

我读过 Oracle 11g 将此作为一项新特性,但不幸的是升级不是一种选择。

Is there any alternative to CONTINUE in 10g? I don't believe it's practical to restructure the logic of the SP as a work-around, because I have an outer loop, an IF, then a nested IF, then the CONTINUE at the end of a statement block within that IF.

在 10g 中是否有任何替代 CONTINUE 的方法?我认为将 SP 的逻辑重构为一种变通方法是不切实际的,因为我有一个外循环、一个 IF、一个嵌套的 IF,然后是该 IF 中语句块末尾的 CONTINUE。

Any help would be greatly appreciated, cheers.

任何帮助将不胜感激,干杯。

回答by jop

You can simulate a continue using goto and labels.

您可以使用goto 和 labels模拟 continue 。

DECLARE
   done  BOOLEAN;
BEGIN
   FOR i IN 1..50 LOOP
      IF done THEN
         GOTO end_loop;
      END IF;
   <<end_loop>>  -- not allowed unless an executable statement follows
   NULL; -- add NULL statement to avoid error
   END LOOP;  -- raises an error without the previous NULL
END;

回答by Christopher McAtackney

Though it's a bit complex and just a fake, you can use exception this way :

虽然它有点复杂而且只是假的,但您可以通过这种方式使用异常:

DECLARE
  i NUMBER :=0;
  my_ex exception;
BEGIN
  FOR i IN 1..10
  LOOP
      BEGIN
         IF i = 5 THEN
            raise my_ex;
         END IF;
         DBMS_OUTPUT.PUT_LINE (i);
      EXCEPTION WHEN my_ex THEN
         NULL;
      END;
  END LOOP;

END;

回答by Sylvain Rodrigue

In fact, PL SQL does have something to replace CONTINUE. All you have to do is to add a label (a name) to the loop :

事实上,PL SQL 确实有一些东西可以替代 CONTINUE。您所要做的就是在循环中添加一个标签(名称):

declare
   i integer;
begin
   i := 0;

   <<My_Small_Loop>>loop

      i := i + 1;
      if i <= 3 then goto My_Small_Loop; end if; -- => means continue

      exit;

   end loop;
end;

回答by eric.itzhak

For future searches, in oracle 11g they added a continuestatement, which can be used like this :

对于未来的搜索,他们在 oracle 11g 中添加了一个continue语句,可以这样使用:

    SQL> BEGIN
  2     FOR i IN 1 .. 5 LOOP
  3        IF i IN (2,4) THEN
  4           CONTINUE;
  5        END IF;
  6        DBMS_OUTPUT.PUT_LINE('Reached on line ' || TO_CHAR(i));
  7     END LOOP;
  8  END;
  9  /
Reached on line 1
Reached on line 3
Reached on line 5

PL/SQL procedure successfully completed.

回答by Dilshod Tadjibaev

It's not available in 10g, however it's a new featurein 11G

它在 10g 中不可用,但它是11G 中的一个新功能

回答by cagcowboy

Can you refactor the IFs into a function, returning at the appropriate point (early if necessary). Then the control flow will pick up in the loop at the right place.

您能否将 IF 重构为一个函数,在适当的点返回(如有必要,尽早返回)。然后控制流将在正确的位置在循环中启动。

Does that make sense?

那有意义吗?

回答by David Oneill

Not exactly elegant, but simple:

不完全优雅,但很简单:

DECLARE
   done  BOOLEAN;
BEGIN
   FOR i IN 1..50 LOOP
      IF done THEN
         NULL;
      ELSE
         <do loop stuff>;
      END IF;
   END LOOP; 
END;

回答by hvb

This isn't exactly an answer to the question, but nevertheless worth noting:

这不完全是问题的答案,但值得注意的是:

The continuestatement in PL/SQL and all other programming languages which use it the same way, can easily be misunderstood.

continuePL/SQL 和所有其他以相同方式使用它的编程语言中的语句很容易被误解。

It would have been much wiser, clearer and more concise if the programming language developers had called the keyword skipinstead.

如果编程语言开发人员skip改为调用关键字,它会更明智、更清晰和更简洁。

For me, with a background of C, C++, Python, ... it has always been clear what `continue' means.

对我来说,有 C、C++、Python 背景的我一直很清楚“继续”是什么意思。

But without that historical background, you might end intepreting this code

但是如果没有那个历史背景,你可能会最终解释这段代码

for i in .. tab_xy.count loop
    CONTINUE WHEN some_condition(tab_xy(i));
    do_process(tab_xy(i));
end loop;

like this:

像这样:

Loop through the records of the table tab_xy.

循环遍历表tab_xy 的记录。

Continue if the record fulfills some_condition, otherwise ignore this record.

如果记录满足 some_condition 则继续,否则忽略此记录。

Do_process the record.

Do_process 记录。

This interpretation is completely wrong, but if you imagine the PL/SQL code as a kind of cooking receipt and read it aloud, this can happen.

这种解释是完全错误的,但如果你把 PL/SQL 代码想象成一种烹饪收据并大声朗读,这可能会发生。

In fact it happened to a very experienced development co-worker just yesterday.

事实上,就在昨天,它发生在一位非常有经验的开发同事身上。

回答by Thorsten

In Oracle there is a similar statement called EXIT that either exits a loop or a function/procedure (if there is no loop to exit from). You can add a WHEN to check for some condition.

在 Oracle 中有一个名为 EXIT 的类似语句,它要么退出循环,要么退出函数/过程(如果没有可退出的循环)。您可以添加 WHEN 来检查某些条件。

You could rewrite the above example as follows:

您可以按如下方式重写上面的示例:

DECLARE
   done  BOOLEAN;
BEGIN
    FOR i IN 1..50 LOOP
     EXIT WHEN done;
   END LOOP;
END;

This may not be enough if you want to exit from deep down some nested loops and logic, but is a lot clearer than a couple of GOTOs and NULLs.

如果您想从一些嵌套循环和逻辑的深处退出,这可能还不够,但比几个 GOTO 和 NULL 更清晰。