GOTO, RETURN, EXIT in Oracle

Source: Internet
Author: User

1. GOTO, mainly used for jump, but will disrupt our program logic. It is generally not used. It can implement the RETURN and EXIT functions.

2. RETURN: RETURN to the end of the program and end the program.

3. EXIT, mainly used to EXIT the current loop, which is equivalent to break in java.

4. You can use custom exception methods to implement the continue function in java.

Comparison between them

  1. -- The GOTO here is equivalent to the following EXIT usage
  2. BEGIN
  3. FOR I IN1..2LOOP
  4. IF I =2THEN
  5. GOTO label;
  6. End if;
  7. Dbms_output.put_line ('I ='| I );
  8. End loop;
  9. <Label>
  10. Dbms_output.put_line ('The last ...');
  11. END;
  12. /
  13. I =1
  14. The last...
  15. PL/SQL procedure successfully completed.
  16. BEGIN
  17. FOR I IN1..2LOOP
  18. IF I =2THEN
  19. -- GOTO label;
  20. EXIT;
  21. End if;
  22. Dbms_output.put_line ('I ='| I );
  23. End loop;
  24. <Label>
  25. Dbms_output.put_line ('The last ...');
  26. END;
  27. /
  28. I =1
  29. The last...
  30. PL/SQL procedure successfully completed.
  31. BEGIN
  32. FOR I IN1..2LOOP
  33. IF I =2THEN
  34. -- GOTO label;
  35. -- EXIT;
  36. RETURN;
  37. End if;
  38. Dbms_output.put_line ('I ='| I );
  39. End loop;
  40. <Label>
  41. Dbms_output.put_line ('The last ...');
  42. END;
  43. /
  44. I =1
  45. PL/SQL procedure successfully completed.
  46. -- The following is equivalent to the above RETURN
  47. BEGIN
  48. FOR I IN1..2LOOP
  49. IF I =2THEN
  50. GOTO label;
  51. -- EXIT;
  52. -- RETURN;
  53. End if;
  54. Dbms_output.put_line ('I ='| I );
  55. End loop;
  56. Dbms_output.put_line ('The last ...');
  57. <Label>
  58. NULL; -- This NULL cannot be omitted. <label> it cannot be earlier than END; end loop;
  59. END;
  60. /
  61. I =1
  62. PL/SQL procedure successfully completed.

Customize exceptions to implement the continue Function

  1. DECLARE
  2. E_My_Exception EXCEPTION;
  3. -- PRAGMA EXCEPTION_INIT (e_My_Exception ,-1401);
  4. BEGIN
  5. FOR I IN1..2LOOP
  6. BEGIN
  7. IF I =2THEN
  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. /

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.