Oracle 中的GOTO,RETURN,EXIT

來源:互聯網
上載者:User

1.GOTO,主要用於跳轉,但會打亂我們的程式邏輯,一般不使用,它可以實現RETURN,EXIT的功能

2.RETURN,返回程式末尾,結束程式

3.EXIT,主要用於退出當前迴圈,相當於java中的break.

4.要實現java中continue的功能可以使用自訂異常的方式

它們之間的比較

  1. --這裡的GOTO相當於下面的EXIT用法   
  2. BEGIN   
  3.    FOR i IN 1..2 LOOP   
  4.       IF i=2 THEN   
  5.          GOTO label;   
  6.       END IF;   
  7.       dbms_output.put_line('i='||i);   
  8.          
  9.    END LOOP;   
  10.    <<label>>   
  11.    dbms_output.put_line('the last...');   
  12. END;   
  13. /   
  14. i=1  
  15. the last...   
  16. PL/SQL procedure successfully completed.   
  17. BEGIN   
  18.    FOR i IN 1..2 LOOP   
  19.       IF i=2 THEN   
  20.          --GOTO label;   
  21.          EXIT;   
  22.       END IF;   
  23.       dbms_output.put_line('i='||i);   
  24.          
  25.    END LOOP;   
  26.    <<label>>   
  27.    dbms_output.put_line('the last...');   
  28. END;   
  29. /   
  30. i=1  
  31. the last...   
  32. PL/SQL procedure successfully completed.   
  33. BEGIN   
  34.    FOR i IN 1..2 LOOP   
  35.       IF i=2 THEN   
  36.          --GOTO label;   
  37.          --EXIT;   
  38.          RETURN;   
  39.       END IF;   
  40.       dbms_output.put_line('i='||i);   
  41.    END LOOP;   
  42.    <<label>>   
  43.    dbms_output.put_line('the last...');   
  44. END;   
  45. /   
  46. i=1  
  47. PL/SQL procedure successfully completed.   
  48. --下面相當於上面的RETURN   
  49. BEGIN   
  50.    FOR i IN 1..2 LOOP   
  51.       IF i=2 THEN   
  52.          GOTO label;   
  53.          --EXIT;   
  54.          --RETURN;   
  55.       END IF;   
  56.       dbms_output.put_line('i='||i);   
  57.    END LOOP;   
  58.    dbms_output.put_line('the last...');   
  59.    <<label>>   
  60.    NULL;--這個NULL不能省略,<<label>>不能在END;END LOOP;等之前   
  61. END;   
  62. /   
  63. i=1  
  64. PL/SQL procedure successfully completed.  

自訂異常的方式實現continue的功能

  1. DECLARE   
  2.   e_My_Exception EXCEPTION;   
  3.   --PRAGMA EXCEPTION_INIT (e_My_Exception, -1401);   
  4. BEGIN   
  5.    FOR i IN 1..2 LOOP   
  6.    BEGIN   
  7.       IF i=2 THEN   
  8.          RAISE e_My_Exception;   
  9.       END IF;   
  10.       dbms_output.put_line('i='||i);   
  11.       EXCEPTION   
  12.           WHEN e_My_Exception THEN   
  13.              NULL;   
  14.    END;   
  15.    END LOOP;   
  16.    dbms_output.put_line('the last...');   
  17. END;   
  18. /  

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.