如何访问类型为TABLE的TABLE中的RECORD中的值Oracle PLSQL

yhqotfr8  于 2023-04-29  发布在  Oracle
关注(0)|答案(1)|浏览(167)
DECLARE
    TYPE a_rec IS RECORD (
        name   VARCHAR2(100),
        adress VARCHAR2(100)
    );
TYPE A_DET IS TABLE OF A_REC;
TYPE A IS TABLE OF
a_det;

BEGIN
    FOR i IN a.first..a.last LOOP
        FOR j IN a_det.first..a_det.last LOOP    
          dbms_output.put_line(a(i).a_det(j).name);
        END LOOP;
    END LOOP;
END;
c0vxltue

c0vxltue1#

您只声明了这些类型,并没有声明任何这些类型的变量。一旦你这样做了,你就可以使用FOR i IN 1 .. variable_name.COUNT LOOP来迭代集合的(非稀疏)元素,并使用variable_name(i)来访问集合元素,使用variable_name(i)(j)来访问嵌套集合:

DECLARE
  TYPE a_rec IS RECORD (
    name    VARCHAR2(100),
    address VARCHAR2(100)
  );
  TYPE A_DET IS TABLE OF A_REC;
  TYPE A IS TABLE OF a_det;
  v_a A := a(
           a_det(
             a_rec('Alice', 'AAA'),
             a_rec('Betty', 'BBB'),
             a_rec('Carol', 'CCC')
           ),
           a_det(
             a_rec('Debra', 'DDD')
           ),
           a_det(),
           a_det(
             a_rec('Emily', 'EEE'),
             a_rec('Fiona', 'FFF')
           )
         );
BEGIN
  FOR i IN 1 .. v_a.COUNT LOOP
    FOR j IN 1 .. v_a(i).COUNT LOOP    
      DBMS_OUTPUT.PUT_LINE(v_a(i)(j).name);
    END LOOP;
  END LOOP;
END;
/

输出:

Alice
Betty
Carol
Debra
Emily
Fiona

fiddle

相关问题