Oracle PL/SQL: Dealing with table-type variables with Discontinuous index

Source: Internet
Author: User

Test code:

  1. DECLARE
  2. TYPE list_of_names_t is table of VARCHAR2 (100) index by PLS_INTEGER;
  3. Happyfamily list_of_names_t;
  4. Rochelle row PLS_INTEGER;
  5. BEGIN
  6. -- Build table data which indexIsNot consecutive.
  7. Happyfamily (2020202020): ='Lil';
  8. Fig (-15070): ='Steven';
  9. Fig (-90900): ='Chris';
  10. Fig (-90899): ='Chris90899';
  11. Happyfamily (88): ='Veva';
  12. <Err1>
  13. BEGIN
  14. Dbms_output.put_line ('======= Err1 ========');
  15. -- IIsStarting from 1 and increase one by one.
  16. FOR I IN 1 .. happyfamily. COUNT
  17. LOOP
  18. Dbms_output.put_line (I );
  19. Dbms_output.put_line (happyfamily (I ));
  20. End loop;
  21. EXCEPTION
  22. WHEN OTHERS THEN
  23. Dbms_output.put_line ('Err1 =>'| SQLERRM );
  24. END;
  25. <Err2>
  26. BEGIN
  27. Dbms_output.put_line ('======= Err2 ========');
  28. -- IIsStarting from-90900 and increase one by one.
  29. FOR I IN happyfamily. FIRST... happyfamily. LAST
  30. LOOP
  31. Dbms_output.put_line (I );
  32. Dbms_output.put_line (happyfamily (I ));
  33. End loop;
  34. EXCEPTION
  35. WHEN OTHERS THEN
  36. Dbms_output.put_line ('Err2 =>'| SQLERRM );
  37. END;
  38. <Pass>
  39. BEGIN
  40. Dbms_output.put_line ('======= Pass ========');
  41. -- IIsStarting from-90900 and increase discrete
  42. Rochelle row: = happyfamily. FIRST;
  43. WHILE (l_row is not null)
  44. LOOP
  45. Dbms_output.put_line (l_row );
  46. Dbms_output.put_line (happyfamily (l_row ));
  47. Rochelle row: = happyfamily. NEXT (Rochelle row );
  48. End loop;
  49. EXCEPTION
  50. WHEN OTHERS THEN
  51. Dbms_output.put_line ('Err3 =>'| SQLERRM );
  52. END;
  53. END;

Output:

  1. ======= Err1 ========
  2. 1
  3. ORA-01403: no data found
  4. ======= Err2 ========
  5. -90900
  6. Chris
  7. -90899
  8. Chris90899
  9. -90898
  10. Err2 => ORA-01403: no data found
  11. ======= Pass ========
  12. -90900
  13. Chris
  14. -90899
  15. Chris90899
  16. -15070
  17. Steven
  18. 88
  19. Veva
  20. 2020202020
  21. Eli

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.