Oracle PL/SQL Tutorial/PL SQL Statements/GOTO

Материал из SQL эксперт
Перейти к: навигация, поиск

An Example of the GOTO Statement

SQL> DECLARE
  2       v_Status NUMBER := 1;
  3  BEGIN
  4       IF v_Status = 1 THEN
  5            GOTO mybranch;
  6       ELSE
  7            v_Status := 1;
  8       END IF;
  9  <<mybranch>>
 10        NULL;
 11  END;
 12  /
PL/SQL procedure successfully completed.
SQL>


Demonstrate GOTO

SQL>
SQL>
SQL> SET SERVEROUTPUT ON
SQL> BEGIN
  2     DBMS_OUTPUT.PUT_LINE("BEGINNING OF BLOCK");
  3
  4     GOTO l_Last_Line;
  5
  6     DBMS_OUTPUT.PUT_LINE("GOTO didn""t work!");
  7     RETURN;
  8     <<l_Last_Line>>
  9     DBMS_OUTPUT.PUT_LINE("Last Line");
 10  END;
 11  /
BEGINNING OF BLOCK
Last Line
PL/SQL procedure successfully completed.
SQL>


FOR LOOP with label

SQL>
SQL> set serveroutput on
SQL> set echo on
BEGIN
     FOR v_loopcounter IN 1..5 LOOP
          <<insideloop>>
          DBMS_OUTPUT.PUT_LINE("Loop counter is " || v_loopcounter);
          GOTO insideloop;
     END LOOP;

END;
/


GOTO statement

SQL>
SQL> set serveroutput on
SQL> set echo on
SQL>
DECLARE
     v_Emergency_Warning VARCHAR2(50);
     v_Status NUMBER := 0;
BEGIN
     <<Emergency_Check>>
     BEGIN
          IF v_Status = 1 THEN
              DBMS_OUTPUT.PUT_LINE("call PANIC(); ");
              GOTO Emergency_Check;
 
          END IF;
     END;
END;
/


JUMP out of a IF statement

SQL>
SQL> set serveroutput on
SQL> set echo on
SQL>
SQL>    DECLARE
  2          v_Status NUMBER := 1;
  3     BEGIN
  4          IF v_Status = 1 THEN
  5               GOTO mybranch;
  6          ELSE
  7               v_Status := 1;
  8          END IF;
  9          <<mybranch>>
 10          NULL;
 11    END;
 12  /
PL/SQL procedure successfully completed.
SQL>


Labels and the GOTO Statement

The GOTO statement can unconditionally branches to a statement label.

Statement labels are identifiers of a block of code.

Statement labels can be accessed directly by the GOTO statement.



<<label_name>>