You are on page 1of 5

COLLECTIONS Scalar -- one value -- a number (3), b varchar2 (10 Ref -- pointer to data -- REF Cursor Composite -- Single

variable can hold multiple values. LOB -- Large Objects...single value...but huge amount -- 4GB Collection is an example of composite data type. Collection is a group of elements of same data type. Individual elements of collections can be accessible using a subscript (index) Collection is equal to Array in C, Java...

a:=a(1,22,33,44,55); a(3) -- 33 -- here 3 is subscript. PL/SQL supports 3 types of collections Nested Table Varray Index-by tables or Associate Arrays Collection can be created at two levels 1. Within a PL/SQL program -- procedure, functions ... triggers...Standalone PL/SQL block 2. At schema level -- using create statement. NESTED TABLE TABLE Number of elements Unlimited Limit unlimited Subscript Nature Number Number Number/Varchar/date Subscript Behavior Sequential/Nonsequential sequential Sequential/Non sequential Where can i create PL/SQL or SQL PL/SQL or SQL PL/SQL Use in Table's column Yes Yes No Initialize Required Yes Yes No VARRAY INDEX BY

If u know no of arrays use Don't know no. of arrays use

-- Varray -- Nested Table or Index by table

Using Collections: 1. Declare a collection type -- In declare section. 2. Declare a variable of collection type and initialize it. (Index by tables is not required)

How can I initialize a Collection? ------------------------------------Using Constructor function, we can initialize it. Constructor function is a function with the same name as your collection name. During the initialization of collection we can specify values to it. Note: Type a is table of number(3); -- collection declaration x a:=a(1,22,33,44,55); -- 5 y a:=a(); -- 0 x.extend; x(6):=123; x.extend(5); -- You are extending for 5 elements Note: Before adding any additional elements to collection, we have to extend that collection. Extension of collection is not required for index by tables. Collection methods: Using collection methods we can get or manipulate collections informations. <collectionvariable>.methodname; count -- returns the number of elements in collection. Ex: a.count extend -- we are extending collection by 1 element. Ex: a.extend extend(n) -- extending collection by n elements. Ex: a.extend(5) first -- returns first index of collection Ex: a.first last -- returns last index of collection. Ex: a.last exists(n) -- To check weather index 5 is available or not. delete(n) -- removes nth element from collection. a.delete(3); -- removing 3rd element from collection. delete -- removes all elements from collections. trim -- removes one element from end of collection. trim(n) -- removes n elements from end of collection. limit -- returns max number of elements we can put into collection. This is mostly useful in Varray only....may not work in nested array Note: If we use method on unsupported collection then we will be getting NULL.

Nested Tables: Syntax: [create or replace] type <typename> is table of <datatype> [NOT NULL]; Note: CREATE OR REPLACE required while creating collection at SQL level. Ex: type a is table of number (3); a is nested table with elements of type number(3) type empnames is table of emp.ename%type; empnames is a nested table with elements of type emp tables ename type type emprows is table of emp%rowtype; emprows is nested table with elements of emp%rowtype. a emprows; a(1) -- first element of emprows collections -- emp%rowtype a(1).ename -- accessing ename value of first row of a. set serveroutput on delcare type a is table of number(3); -- a is nested table x a:=a(22,45,34,66,77); -- x is variable of collection a begin dbms_output.put_line('Count of X is' || x.count); end;

set serveroutput on delcare type a is table of number(3); -- a is nested table x a:=a(22,45,34,66,77); -- x is variable of collection a begin dbms_output.put_line('Count of X is' || x.count); x.extend; x(6):=100; dbms_output.put_line('6th element of collections' || x(6)); end; -- Displaying all elements of Collection:

set serveroutput on declare type a is table of number(3); a is a nested table x a:=a(23,45,33,66,77); -- x is a variable of collection a begin for i in 1....x.count loop dbms_output.put_line('elements at' || i || 'is' || x(i)); end loop; end; set serveroutput on declare type a is table of number(3); a is a nested table x a:=a(23,45,33,66,77); -- x is a variable of collection a begin for i in x.first....x.count loop dbms_output.put_line('elements at' || i || 'is' || x(i)); end loop; end; from table to collection -- process within collections -- finally load bacl into table from collection Write a PL/SQL program for displaying ename column values from emp table using collection. set server output on declare begin type empname is table of emp.ename%type; x empnames:=x(); cursor c is select empnames from emp; j number(10) := 1; -- index of collection begin -- Loading values from cursor into collection for i in c loop x.extend; x(j):=i.ename; j:=j+1; end loop;

-- displaying collection elements for k in 1....x.count loop dbms_output.put_line('ename at' || k || 'is' || x(k)); end loop; end; Copy data from emp to emp_bkp table using collections.

You might also like