PLSQL

PL/SQL Collections

PL/SQL Collections

Collections are said to be an ordered group of elements that has the same data type. All the elements are identified by a unique subscript that represents its position in the collection. 

PL/SQL has provided three collection types:

  1. Index-by tables or Associative array
  2. Nested table
  3. Variable-size array or Varray

 

INDEX-BY TABLE 

It is also known as an associative array, is said to be a key-value pairs. This key is said to be unique and used for locating the corresponding value. Key can be either an integer or the string. This table is created using the following syntax. 

TYPE type_name IS TABLE OF element_type [NOT NULL] INDEX BY subscript_type;

table_name type_name; 
Example:
DECLARE 
   CURSOR t_teachers is 
      select name from teachers; 
   TYPE t_list IS TABLE of teachers.Name%type INDEX BY binary_integer; 
   name_list t_list; 
   counter integer :=0; 
BEGIN 
   FOR n IN t_teachers LOOP 
      counter := counter +1; 
      name_list(counter) := n.name; 
      dbms_output.put_line('teacher('||counter||'):'||name_list(counter)); 
   END LOOP; 
END; 
/ 
Output:
teacher(1): Ranjitha 
teacher(2): sai
teacher(3): shakthi     
teacher(4): gayathri  
teacher(5): vignesh
PL/SQL procedure successfully completed

 

NESTED Tables

This is like one-dimensional array with the arbitrary number of elements. A nested table differs from the array in the following given aspects:

  1. When an array has declared number of elements, but the nested table does not have. Then the size of the table increases dynamically.
  2. Array is said to be always dense, that has consecutive subscripts. The nested array is said to be dense initially, but this can become sparse when the elements are deleted from it.

A Nested table can be created by using the given below Syntax:

Syntax:
TYPE type_name IS TABLE OF element_type [NOT NULL];
table_name type name;
The declaration is said to be the declaration on an index-by, but there is no INDEX BY clause. This table can be stored in the database column. 
Example:
DECLARE 
   CURSOR t_teachers is  
      SELECT  name FROM teachers;  
   TYPE t_list IS TABLE of teacherS.No.ame%type; 
   name_list t_list := c_list(); 
   counter integer :=0; 
BEGIN 
   FOR n IN t_teachers LOOP 
      counter := counter +1; 
      name_list.extend; 
      name_list(counter)  := n.name; 
      dbms_output.put_line('teacher('||counter||'):'||name_list(counter)); 
   END LOOP; 
END; 
/
Output:
teacher(1): Ranjitha 
teacher(2): sai
teacher(3): shakthi     
teacher(4): gayathri  
teacher(5): vignesh
PL/SQL procedure successfully completed

Collection Methods

Given below are the collection methods that makes easier for using collections.

  • EXISTS(n) – this trues if nth element in a collection exists, else it returns false.
  • COUNT – this returns the number of elements that a collection currently contains.
  • LIMIT – it checks the maximum size of the collections.
  • FIRST – it returns the first that is the smallest index numbers in the collection that makes use of integer subscripts.
  • LAST - it returns the last that is the largest index numbers in the collection that makes use of integer subscripts.
  • PRIOR(n) – this returns the index number that precedes index n in a collection.
  • NEXT(n) – this returns the index number that succeeds index n.
  • EXTEND – it appends one null elements to a collection.
  • EXTEND(n) –  it appends n null elements to collections.
  • EXTEND(n, i) - it appends n copies of the ith element to a collection.
  • TRIM – this removes one element from the end of the collection.
  • TRIM(n) – this removes n elements from the end of the collection.
  • DELETE – this removes all the elements from the collection, setting COUNT to 0.
  • DELETE(n) – this removes the nth element from an associative array with the numeric key or a nested table. If n is null, then this method does nothing.
  • DELETE(M, N) – removes all the elements in the range m..n from an associative array or the nested table. If m is said to be larger than n or if m or n is null DELETE(m,n) does nothing.

Collection Exceptions

Below are the exceptions and when they are raised:

  1. COLLECTION_IS_NULL – when we try to operate on automatically null collection.
  2. NO_DATA_FOUND – the subscript that designates the elements that are deleted, or a nonexistent element of an associative array.
  3. SUBSCRIPT_BEYOND_COUNT – when the subscript exceeds the number of elements in the collection. 
  4. SUBSCRIPT_OUTSIDE_LIMIT – when the subscript is said to be the outside the allowed range.
  5. VALUE_ERROR – a subscript is said to be null, or those that are not convertible to the key type. This might occur only if the key is defined like PLS_INTEGER range, and then when subscript is said to be outside this range.