René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Array of arrays with PL/SQL

set serveroutput on size 1000000

create or replace package array_of_array as
  type vc_array is table of varchar2(100) index by pls_integer;
  type vc_array_array is table of vc_array index by pls_integer;

  function p return vc_array_array;
end array_of_array;
/

create or replace package body array_of_array as
  function p return vc_array_array is
    ret vc_array_array;
  begin
    ret(1)(10) := '10';
    ret(2)(15) := '30';
    ret(2)( 4) :=  '8';
    ret(3)( 2) :=  '6';
    ret(3)( 9) := '27';
    ret(7)( 4) := '28';
    ret(6)( 6) := '36';
    ret(1)( 8) :=  '8';
    

    return ret;
  end p;
end array_of_array;
/


declare
  x array_of_array.vc_array_array;
  v_i pls_integer;
  v_j pls_integer;
begin
  x:=array_of_array.p;

  v_i := x.first;
  while v_i is not null loop
    v_j := x(v_i).first;
    while v_j is not null loop
      dbms_output.put_line(v_i || ' x ' || v_j  || ' = ' ||  x(v_i)(v_j));
      v_j := x(v_i).next(v_j);
    end loop;
    v_i := x.next(v_i);
  end loop;
end;
/
set feedback off
create or replace type vc_array is table of varchar2(100);
/

create or replace type vc_array_array is table of vc_array;
/


declare
  array_of_array vc_array_array;
begin
  array_of_array := vc_array_array();

  array_of_array.extend;
  array_of_array(1) := vc_array('one', 'two', 'three');

  array_of_array.extend;
  array_of_array(2) := vc_array('un', 'deux', 'trois');

  array_of_array.extend;
  array_of_array(3) := vc_array('eins', 'zwei', 'drei');

  for i in 1 .. array_of_array.count loop
    for j in 1 .. array_of_array(i).count loop
      dbms_output.put(lpad(array_of_array(i)(j),6));
    end loop;
    dbms_output.put_line('');
  end loop;
end;
/

drop type vc_array_array;
drop type vc_array;