Saturday, March 30, 2013

Associative Array and it's collection Methods


CREATE OR REPLACE PROCEDURE xx_AssocArr
AS

TYPE xx_AsscArr IS TABLE OF VARCHAR2(20)
INDEX BY PLS_INTEGER;

TYPE xx_AsscArr1 IS TABLE OF NUMBER
INDEX BY PLS_INTEGER;

TYPE xx_AsscArr2 IS TABLE OF VARCHAR2(40)
INDEX BY VARCHAR2(20);

TYPE xx_AsscArr3 IS TABLE OF NUMBER
INDEX BY VARCHAR2(20);

V1 xx_AsscArr;

V2 xx_AsscArr1;

V3 xx_AsscArr2;

V4 xx_AsscArr3;

i NUMBER;
J NUMBER;
z VARCHAR2(30);

BEGIN

dbms_output.put_line('Hi..');

/*Like in VARRAY and NESTED TABLE 
associative array doesn't require 
initialization directly values
can be assigned*/

--These are also called as key-value pairs

--v1 has values of string type and indexed by integer

v1(1000):='Vizag';

v1(1500):='Hyderabad';

v1(-100):='Atlanta';

dbms_output.put_line('FIRST INDEX of V1 : '||v1.FIRST);
dbms_output.put_line('LAST INDEX of V1 : '||v1.LAST);
dbms_output.put_line('count of V1 : '||v1.COUNT);

IF (v1.EXISTS(-100)) THEN
   dbms_output.put_line('Element exists at index -100 : '|| v1(-100));
ELSE
   dbms_output.put_line('Element does not exist');
END IF;

dbms_output.put_line('Index prior to element at 1000 : '||v1.PRIOR(1000));

dbms_output.put_line('Index next to element at 1000 : '||v1.NEXT(1000));

dbms_output.put_line('Printing V1');

j:=v1.FIRST;

WHILE j is NOT NULL LOOP
dbms_output.put_line('Element at index # '||j||' '||v1(j));
        j:=v1.NEXT(j);
END LOOP;

v1.DELETE(1000);

dbms_output.put_line('Printing V1');

j:=v1.FIRST;

WHILE j is NOT NULL LOOP
dbms_output.put_line('Element at index # '||j||' '||v1(j));
        j:=v1.NEXT(j);
END LOOP;

dbms_output.put_line('Working on V3');

/*v3 has values of string and indexed by strings!!
*/

v3('vizag'):='This is vizag city';

v3('pune'):='This is pune city';

v3('banglore'):='This is blore city';

dbms_output.put_line('Printing V3');

z:=v3.FIRST;

WHILE z is NOT NULL LOOP
dbms_output.put_line('Element at index # '||z||' '||v3(z));
        z:=v3.NEXT(z);
END LOOP;

v3.DELETE('pune');

dbms_output.put_line('Printing V3');

z:=v3.FIRST;

WHILE z is NOT NULL LOOP
dbms_output.put_line('Element at index # '||z||' '||v3(z));
        z:=v3.NEXT(z);
END LOOP;


EXCEPTION

WHEN OTHERS THEN

dbms_output.put_line('Error is : '||SQLERRM);

END xx_AssocArr;
/

Comparison of PL/SQL COLLECTIONS - III

NAME VARRAY NESTED TABLE ASSOCIATIVE ARRAY
TRIM
Removes one element from last
in a collection
v1.TRIM v1.TRIM NA
TRIM(n)
Removes n elements from last
in a collection
v1.TRIM(3) v1.TRIM(3) NA
DELETE
Deletes all the elements
in a collection
v1.DELETE v1.DELETE v1.DELETE
DELETE(n)
Deletes elements at index n
in a collection
NA v1.DELETE(6) v1.DELETE(6)
DELETE(m,n)
Deletes all the elements
between m and n indexes
in a collection
NA v1.DELETE(4,7) v1.DELETE(4,7)

Comparison of PL/SQL COLLECTIONS - II

NAME VARRAY NESTED TABLE ASSOCIATIVE ARRAY
DECLARATION TYPE VarTab IS VARRAY(10) of NUMBER; TYPE VarTab IS TABLE OF NUMBER; TYPE xx_AsscArr IS TABLE OF VARCHAR2(20)
INDEX BY PLS_INTEGER;
INITIALIZATION v1 := VarTab(); v1 := VarTab(); NA
ASSIGNMENT v1.extend();
v1(1):=1001
v1.extend();
v1(1):=1001
v1(1):='Vizag';
LIMIT
Gives Max length of collection
v1.LIMIT NA as it is unbounded NA as it is unbounded
EXTENDS
Allocates one NULL element
to a collection
v1.EXTENDS v1.EXTENDS NA
EXTENDS(n)
Allocates n NULL elements
to a collection
v1.EXTENDS(5) v1.EXTENDS(5) NA
EXTENDS(n,i)
Allocates n elements
with valueof element at index i collection
v1.EXTENDS(4,1) v1.EXTENDS(4,1) NA
EXISTS(n)
Returns TRUE if element exists
at Index n else returns FALSE
v1.EXISTS(-100) v1.EXISTS(-100) v1.EXISTS(-100)
COUNT
Returns total no of elements
in a collection
v1.COUNT v1.COUNT v1.COUNT
FIRST
Returns the index of the
first element in the collection
v1.FIRST v1.FIRST v1.FIRST
LAST
Returns the index of the
last element in the collection
v1.LAST v1.LAST v1.LAST
PRIOR(n)
Returns the index of the element prior to the
current element
v1.PRIOR(n) v1.PRIOR(n) v1.PRIOR(n)
NEXT(n)
Returns the index of the element next to the
current element
v1.NEXT(n) v1.NEXT(n) v1.NEXT(n)

Comparison of PL/SQL COLLECTIONS - I

NAME VARRAY NESTED TABLE ASSOCIATIVE ARRAY
SIZE Bounded Unbounded Unbounded
DENSE/SPARSE DENSE Both Both
INDEX/SUBSCRIPT DATATYPE Integer Integer Integer/String
WHERE CREATED pl/sql block or at schema level pl/sql block or at schema level Only in a pl/sql block
CAN BE OBJECT TYPE ATTRIBUTE Yes No No