Monday 28 October 2013

How to Initialization collections?

Only Nested tables and varrays need initialization.
To initialize a collection, you use the “constructor” of the collection which name is the same as the collection.

Nested tables
Declare
   TYPE TYP_NT_NUM IS TABLE OF NUMBER;
    Nt_tab TYP_NT_NUM;
Begin
   Nt_tab := TYP_NT_NUM( 5, 10, 15, 20 ) ;
End;

Varrays
Declare
   TYPE TYP_V_DAY IS VARRAY(7) OF VARCHAR2(15);
   v_tab TYP_V_DAY;
Begin
   v_tab := TYP_NT_NUM( ‘Sunday’,’Monday’,’Tuesday’,’Wedneday’,’Thursday’,’Friday’,’Saturday’ ) ;
End;

It is not required to initialize all the elements of a collection. You can either initialize no element. In this case, use an empty constructor.
v_tab := TYP_NT_NUM();

This collection is empty, which is different than a NULL collection (not initialized).

Index-by tables
Declare
   TYPE TYP_TAB IS TABLE OF NUMBER INDEX BY PLS_INTEGER;
   my_tab TYP_TAB;
Begin
   my_tab(1) := 5 ;
   my_tab(2) := 10 ;
   my_tab(3) := 15 ;
End;

No comments:

Post a Comment