'CONTINUE' keyword in Oracle 10g PL/SQL

后端 未结 9 2162
失恋的感觉
失恋的感觉 2020-12-14 06:00

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

I\'ve read that Oracle 11g has this a

相关标签:
9条回答
  • 2020-12-14 06:38

    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.

    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.

    0 讨论(0)
  • 2020-12-14 06:42

    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 :

    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;
    
    0 讨论(0)
  • 2020-12-14 06:43

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

        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.
    
    0 讨论(0)
  • 2020-12-14 06:43

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

    0 讨论(0)
  • 2020-12-14 06:47

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

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

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

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

    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.

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

    Do_process the record.

    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.

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

    0 讨论(0)
  • 2020-12-14 06:51

    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;
    
    0 讨论(0)
提交回复
热议问题