嵌套表包括两个基本成分:

    • 标量或记录数据类型的列

    语法如下:

    PL 嵌套表表类型支持的取值方法如下:

    注意

    PL 表的记录被删除后,其他记录的行号不会因此移动。如下例所示:

    1. -> TYPE T_dept_table IS TABLE OF
    2. -> departments%ROWTYPE INDEX BY BINARY_INTEGER;
    3. -> TAB_department T_dept_table;
    4. -> v_count number(2) :=6;
    5. -> BEGIN
    6. -> -- 为表赋值
    7. -> FOR int IN 1 .. v_count LOOP
    8. -> SELECT * INTO TAB_department(int) FROM departments WHERE department_id=int*10;
    9. -> END LOOP;
    10. ->
    11. -> -- 利用COUNT打印总行数
    12. -> DBMS_OUTPUT.PUT_LINE(TAB_department.COUNT||' ROW(S): ');
    13. -> -- 利用FIRSTLAST打印所有行
    14. -> FOR int IN TAB_department.FIRST .. TAB_department.LAST LOOP
    15. -> DBMS_OUTPUT.PUT_LINE('Department number: '||TAB_department(int).department_id);
    16. -> DBMS_OUTPUT.PUT_LINE('Department name: '|| TAB_department(int).department_name);
    17. -> END LOOP;
    18. ->
    19. -> -- 利用EXISTS检查记录
    20. -> IF TAB_department.EXISTS(5) THEN
    21. -> ELSE
    22. -> DBMS_OUTPUT.PUT_LINE('ROW 5 NOT EXISTS');
    23. ->
    24. -> -- DELETE删除一个范围
    25. -> DBMS_OUTPUT.PUT_LINE('Delete row 2-3');
    26. -> TAB_department.DELETE(2,3);
    27. ->
    28. -> -- NEXT从前向后打印记录
    29. -> DBMS_OUTPUT.PUT_LINE('Looping from first');
    30. -> v_count := 1;
    31. -> WHILE v_count IS NOT NULL
    32. -> LOOP
    33. -> DBMS_OUTPUT.PUT_LINE(TAB_department(v_count).department_id);
    34. -> v_count := TAB_department.next(v_count);
    35. -> END LOOP;
    36. ->
    37. -> -- PRIOR从后向前打印,注意此时COUNT4
    38. -> DBMS_OUTPUT.PUT_LINE('Looping from last');
    39. -> v_count := TAB_department.COUNT;
    40. -> WHILE v_count IS NOT NULL
    41. -> LOOP
    42. -> DBMS_OUTPUT.PUT_LINE(TAB_department(v_count).department_id);
    43. -> v_count := TAB_department.prior(v_count);
    44. -> END LOOP;
    45. Query OK, 0 rows affected (0.18 sec)
    46. 6 ROW(S):
    47. Department number: 10
    48. Department name: Administration
    49. Department number: 20
    50. Department name: Marketing
    51. Department number: 30
    52. Department name: Purchasing
    53. Department number: 40
    54. Department name: Human Resources
    55. Department number: 50
    56. Department name: Shipping
    57. Department number: 60
    58. Department name: IT
    59. ROW 5 EXISTS
    60. Delete row 2-3
    61. Looping from first
    62. 10
    63. 40
    64. 50
    65. 60
    66. Looping from last