Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Looping through array

jpvybesNov 1 2010 — edited Nov 2 2010
If I declare an array such as the one below.
declare
type array_type is table of varchar2(100) index by binary_integer;
component_array array_type;

begin
  component_array(12345) := 'One';
  component_array(12347) := 'Two';
  component_array(12349) := 'Three';
  
  for i in component_array.FIRST .. component_array.LAST
  loop
    dbms_output.put_line(component_array(i));
  end loop;

end;
Is there a way to loop through only the existing binary_integer indeces? For example, if you execute the block above, you'll get an error because the loop iteration is hitting 12346 which of course doesn't exist. Other than the workaround below, is there another way using any type of array method?
declare
type array_type is table of varchar2(100) index by binary_integer;
component_array array_type;

begin
  component_array(12345) := 'One';
  component_array(12347) := 'Two';
  component_array(12349) := 'Three';
  
  for i in component_array.FIRST .. component_array.LAST
  loop
    begin
      dbms_output.put_line(component_array(i));
    exception
      when NO_DATA_FOUND then 
        null;
    end;
  end loop;

end;

Thanks.
This post has been answered by Solomon Yakobson on Nov 1 2010
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 30 2010
Added on Nov 1 2010
11 comments
20,436 views