VARRAY
VARRAYs are enhanced version of nested tables, which were introduced in Oracle 8i. They contain limited and defined number of densely populated elements, whose index is sequential and numeric. The minimum bound of the index is 1, current bound is the total number of resident elements and maximum bound is the varray size. At any moment, current bound cannot exceed the maximum bound.
In terms of handling and usage, they are similar to nested tables in major situations. VARRAYs can exist as schema objects, referenced from PL/SQL blocks, and can be stored in database tables.
Unlike nested tables, which are always stored as separate storage table, varrays exist as single object in the same table. Only in exceptional cases when size of varray exceeds 4KB, it is relocated outside the table but in the same tablespace.
Syntax [3a]
In SQL
Sample Code
CREATE [OR REPLACE] TYPE type_name IS {VARRAY | VARYING ARRAY} (size_limit) OF element_type
Copyright exforsys.com
Syntax [3b]
In PL/SQL
Sample Code
DECLARE
TYPE type_name IS {VARRAY | VARYING ARRAY} (size_limit) OF element_type [NOT NULL];
Copyright exforsys.com
Size_limit represents the maximum count of elements in the array. Being a persistent form of collection, varrays require initialization.
Element_type can be one of the data type available till Oracle 11g.
VARRAYS in PL/SQL
Example [6a]
VARRAYs can be locally declared in a PL/SQL block and can be referenced within the scope of the block.
Sample Code
DECLARE
TYPE EX_VRR_TAB IS VARRAY(5) OF VARCHAR2(100);
L_BATSMAN EX_VRR_TAB := EX_VRR_TAB();
L_WICKET EX_VRR_TAB := EX_VRR_TAB();
L_BOWLER EX_VRR_TAB := EX_VRR_TAB();
BEGIN
L_BATSMAN := EX_VRR_TAB(Sachin,Sehwag,Gambhir,Kohli,Yuvraj,);
L_WICKET := EX_VRR_TAB(Dhoni,);
L_BOWLER := EX_VRR_TAB(Bhajji,Nehra,Zaheer,Praveen,Munaf);
END;
Copyright exforsys.com
VARRAYs in SQL
Example [6b]
The example below shows the creation of a VARRAY as schema object
Sample Code
SQL> CREATE OR REPLACE TYPE NUM_VARRAY AS VARRAY(10) OF INTEGER;
/
Type created.
Copyright exforsys.com
Example [6c]
The VARRAY created in the schema can be referenced in a PL/SQL block
Sample Code
DECLARE
L_INTEGER NUM_VARRAY := NUM_VARRAY();
BEGIN
FOR I IN 1..5
LOOP
L_INTEGER.EXTEND;
L_INTEGER(I) := i*2;
END LOOP;
END;
Copyright exforsys.com
Example [6d]
The VARRAY created can be used as a column type. The example demonstrates the Insert and Update process.
Sample Code
SQL> CREATE OR REPLACE TYPE SALES_TYPE AS OBJECT ( SALES_DATE DATE,
SALES_QTY NUMBER);
Type created.
SQL> CREATE OR REPLACE TYPE SALES_VARRAY AS VARRAY(10) OF SALES_TYPE;
Type created.
SQL> CREATE TABLE ORDERS (ORD_ID NUMBER,
ITEM_CODE VARCHAR2(100),
ITEM_SALES SALES_VARRAY);
TABLE created.
SQL> INSERT INTO ORDERS (SEQ_ORD.NEXTVAL,
ELEC,
SALES_VARRAY(
SALES_TYPE(SYSDATE,130),
SALES_TYPE(SYSDATE-5,57),
SALES_TYPE(SYSDATE-10,130)
)
);
1 row inserted.
SQL>DECLARE
L_SALES SALES_VARRAY ;
BEGIN
SELECT ITEM_SALES
INTO L_SALES
FROM ORDERS
WHERE ORD_ID=100
FOR UPDATE OF ITEM_SALES;
L_SALES(2).SALES_QTY := 140 ; -- update the second element
UPDATE ORDERS
SET ITEM_SALES = L_SALES
WHERE ORD_ID=100;
END ;
/
PL/SQL block successfully completed.
SQL> COMMIT;
Commit complete.
Copyright exforsys.com
This tutorial is part of a Oracle 11g Tutorial tutorial series. Read it from the beginning and learn yourself.
H I D E