Collections
Oracle collections are used to store data of the same datatype. There are basically three types of collections.
  1. Index by table
  2. Nested Table
  3. Varray
  4. Records
Syntax Declaration
  
  1. Index by table:
  2. declare type my_tbl_typ is table of char index by binary_integer; my_tbl my_tbl_typ; begin my_tbl(0):=0; my_tbl(1):=1; my_tbl(2):=2; dbms_output.put_line(my_tbl(2) ); end;
  3. Nested Table
  4. declare type my_tbl_typ is table of char; my_tbl my_tbl_typ; begin my_tbl := my_tbl_typ( '0','1','2'); dbms_output.put_line(my_tbl(2) ); end;
  5. Varray
  6. declare type my_tbl_typ is varray(10) of char; my_tbl my_tbl_typ; begin my_tbl := my_tbl_typ( '0','1','2'); dbms_output.put_line(my_tbl(2) ); end;
Referencing
  A index by table is initially initialised to null, therefore
  before accessing the element we need to initialise the value.
  If you try to access a value in a index by table which has not
  been assigned a value earlier, we would get a no data found error.
  For example in the above example if we had used
  dbms_output.put_line ( my_tbl(11)) ;
  we would get the no data found error.
  The first element in a index by table gets a subscript of 0.
  Yes the subscript in index by tables can be negative as well.
  my_tbl(-11):=11;
  dbms_output.put_line ( my_tbl(-11)) ;

  Index by table size is not determined at the time of creation
  for example it is perfectly legal to say
  my_tbl(1000):=1000;
  A index by table cannot be stored in the database.



  A nested table and varray are null by default. We have to use a constructor to
  initialise them for example :
  my_tbl := my_tbl_typ( '0','1','2');  
  The first element gets a subscript of 1.
  Which means my_tbl(0) is invalid and my_tbl(1)=>0.
  A nested table and varray by table can be stored in the database.
  
Assigning and Comparison
  Assignment :
    Index by tables can be assigned by direct assignment for example
    using the same example as above a index by table assignment can
    be done as:
    declare
	  type my_tbl_typ is table of char index by binary_integer;
	  my_tbl my_tbl_typ;
	  my_tbl_2 my_tbl_typ;
	begin
	  my_tbl(0):=0;
	  my_tbl(1):=1;
	  my_tbl(-11):=2;
	  my_tbl_2:=my_tbl;
	  dbms_output.put_line(my_tbl_2(-11) );
    end;

    A index by table and varray can also be assigned in the same way as
    above.
    
Caution:Collections (any three of the above) can never be used for comparison directly for example a statement such as : if ( my_tbl = my_tbl_2 ) --Invalid this will raise a compilation error. then null; end if;
Collection Methods
    The following are the collection methods:
    EXISTS -- returns true if nth element exists or false.
       IF my_tbl.EXISTS(0) THEN my_tbl(0) := 2; END IF;
    COUNT  -- gives the count of the collection.
       FOR i IN 1..my_tbl.COUNT LOOP 
    LIMIT  -- Max. that an collection can contain, for nested tables returns null.
    FIRST AND LAST -- Return index number in a collection, if uninitalised return null.
    PRIOR AND NEXT  -- Return the preceding and next elements index.
    EXTEND  -- Used to extend a collection in varray. (NOT Applicable to index by table.)
    TRIM  -- Removes n elements from an array.
    DELETE -- DELETE removes all elements from a collection.
    Of the above methods only , EXTEND and TRIM cannot be used with index-by tables
Records
     A record stores a group of elements in a structured format.
     Example:
     Records cannot be tested for nullity, equality, or inequality.
     For instance, the following IF conditions are illegal
Hosted by www.Geocities.ws

1