-----------------------------------------------------------------
-- PL/SQL: PL/SQL Tables (Section 4.8)                          -
-- Chapter 4; Oracle Programming -- A Primer                    -
--            by R. Sunderraman                                 -
-----------------------------------------------------------------

DECLARE
  TYPE dd_rec_type IS RECORD (
    table_name varchar2(30),
    column_name varchar2(30),
    data_type  varchar2(9));
  TYPE dd_table_type IS TABLE OF dd_rec_type 
       INDEX BY BINARY_INTEGER;

  dd_table dd_table_type;
  dd_rec dd_rec_type;
  cursor c1 is
     select table_name,column_name,data_type
     from   user_tab_columns
     where  table_name like 'Z%'
     order  by table_name,column_name;
  i binary_integer;
  prev varchar2(30);
  newentry boolean;

BEGIN
  i := 0;
  open c1;
  loop
    fetch c1 into dd_rec;
    exit when c1%NOTFOUND;
    i := i + 1;
    dd_table(i) := dd_rec;
  end loop;
  close c1;
  dbms_output.put_line('Tables Starting with "Z"');
  dbms_output.put_line('------------------------');
  prev := '';
  newentry := false;
  for i in 1 .. dd_table.count loop
    if (i = 1) then
      dbms_output.put(dd_table(i).table_name || '(');
      prev := dd_table(i).table_name;
    elsif (prev != dd_table(i).table_name) then
      dbms_output.put_line(');');
      dbms_output.put(dd_table(i).table_name || '(');
      prev := dd_table(i).table_name;
      newentry := false;
    end if;
    if (newentry) then
      dbms_output.put(', ');
    end if;
    newentry := true;
    dbms_output.put(dd_table(i).column_name || ':');
    dbms_output.put(dd_table(i).data_type);
  end loop;  
  dbms_output.put_line(');');
END;
/
