You are on page 1of 9

DELETE

The DELETE
operation leaves the collection sparse. Any reference to the deleted
index would raise a
NO_DATA_FOUND exception. The DELETE method raises a
COLLECTION_IS_NULL exception
for uninitialized collections. It can be used with all three types of
collections

Oracle doesn’t allow the deletion of individual elements in a varray


collection. Either all
the elements of a varray are removed using the [VARRAY].DELETE method
or the elements
can be trimmed from the end of the varray collection:

Varray
Varrays were introduced in Oracle8i as a modified format of a nested table. The
varray or variable size arrays are bounded and the persistent form of collection
whose major operational features resemble nested tables

. The minimum bound of


the index is 1, current bound is the total number of resident elements and maximum
bound is the varray size.

They are stored in line with their


parent record as a raw value in the parent table. The inline storage mechanism no
more needs a storage clause specification, unique identifier or separate storage table.

The inline storage mechanism of varrays helps Oracle to reduce


the number of IOs on the disk. This makes varrays superior and
more performance efficient than nested tables.

The syntax for varrays, when defined as a database collection type, is as follows:
CREATE [OR REPLACE] TYPE type_name IS {VARRAY | VARYING ARRAY} (size_
limit) OF element_type

In PL/SQL, varrays can be declared as follows:


DECLARE
TYPE type_name IS {VARRAY | VARYING ARRAY} (size_limit) OF
element_type [NOT NULL];

Similar to nested tables, varrays too follow object orientation. For this reason, varrays
require initialization mandatorily before accessing them in the executable section of
the PL/SQL block

SQL> DESC ABC_VARRAY_TAB;

Name Null? Type

----------------------------------------- -------- ----------------------------

ID NUMBER

DID ABC_VARRAY

SQL> INSERT INTO ABC_VARRAY_TAB VALUES(3,ABC_VARRAY(4,5,6));


SELECT query selects the varray in an instance format:
SQL> SELECT * FROM tab_use_va_col;
ID NUM
---------- ------------------------------
1 NUM_VARRAY_T(10, 12, 13)
2 NUM_VARRAY_T(32, 23, 76, 27)

SELECT T.id, T1.column_value

FROM ABC_VARRAY_TAB T, TABLE(T.DID)

ID COLUMN_VALUE

------ ------------

1 1

1 2

1 3

1 4

2 3

2 4

2 5

2 6

2 6

3 4

3 5

ID COLUM

N_VALUE

------ ------------

3 6
ows selected.

ED

e file afiedt.buf

SELECT T.id, T1.*

FROM ABC_VARRAY_TAB T, TABLE(T.DID)

ID COLUMN_VALUE

------ ------------

1 1

1 2

1 3

1 4

2 3

2 4

2 5

2 6

2 6

3 4

3 5
In case of varrays, a single element cannot be updated using the TABLE expression.
The reason is the different storage philosophy of varrays. A varray is stored in line
with the parent row and not as separate storage tables, as in the case of nested tables.
Therefore, a single element of a varray can be updated only through a PL/SQL block.

Nested table versus varrays

Varrays are used in scenarios when the element count is fixed and sequential
access of elements is expected. For example, Address of employees is fixed to
three lines and must be accessed sequentially to maintain its credibility. Nested
tables provide untidy access to all elements where a user can delete or insert
elements simultaneously.

EXISTS
The EXISTS function checks the existence of an element in a collection

It takes the subscript as an input


argument and searches it in the collection. If the element corresponding to the index
is found, it returns TRUE or else, returns FALSE. It is the only method which doesn't
raise any exception during its application with an uninitialized collection.

1 DECLARE
2 /*Declare a local varray type, define collection variable and
3 initialize it*/
4 TYPE V_COLL_DEMO IS VARRAY(4) OF VARCHAR2(100);
5 L_LOCAL_COLL V_COLL_DEMO ;
6 BEGIN
7 L_LOCAL_COLL := V_COLL_DEMO('Oracle 9i',
8 'Oracle 10g',
9 'Oracle 11g');
10 /*Use FOR loop to parse the array variable and print the elements*/
11 FOR I IN 1..L_LOCAL_COLL.COUNT
12 LOOP
13 DBMS_OUTPUT.PUT_LINE(I ||' '||'Printing Oracle version:' ||L_LOCAL_COLL(I));
14 END LOOP;
15 IF L_LOCAL_COLL.EXISTS(1) THEN
16 DBMS_OUTPUT.PUT_LINE('PRESENT');
17 ELSE
18 DBMS_OUTPUT.PUT_LINE('ABSENT');
19 END IF;
20* END;
SQL> /
1 Printing Oracle version:Oracle 9i
2 Printing Oracle version:Oracle 10g
3 Printing Oracle version:Oracle 11g
PRESENT

PL/SQL procedure successfully completed.

SQL> ED
Wrote file afiedt.buf

1 DECLARE
2 /*Declare a local varray type, define collection variable and
3 initialize it*/
4 TYPE V_COLL_DEMO IS VARRAY(4) OF VARCHAR2(100);
5 L_LOCAL_COLL V_COLL_DEMO ;
6 BEGIN
7 L_LOCAL_COLL := V_COLL_DEMO('Oracle 9i',
8 'Oracle 10g',
9 'Oracle 11g');
10 /*Use FOR loop to parse the array variable and print the elements*/
11 FOR I IN 1..L_LOCAL_COLL.COUNT
12 LOOP
13 DBMS_OUTPUT.PUT_LINE(I ||' '||'Printing Oracle version:' ||L_LOCAL_COLL(I));
14 END LOOP;
15 IF L_LOCAL_COLL.EXISTS(4) THEN
16 DBMS_OUTPUT.PUT_LINE('PRESENT');
17 ELSE
18 DBMS_OUTPUT.PUT_LINE('ABSENT');
19 END IF;
20* END;
SQL> /
1 Printing Oracle version:Oracle 9i
2 Printing Oracle version:Oracle 10g
3 Printing Oracle version:Oracle 11g
ABSENT

PL/SQL procedure successfully completed.


ALL METHODS COVER IN BELOW EXAMPLE

1 DECLARE
2 /*Declare a local varray type, define collection variable and
3 initialize it*/
4 TYPE V_COLL_DEMO IS VARRAY(4) OF VARCHAR2(100);
5 TYPE V_COLL_DEM IS VARRAY(4) OF VARCHAR2(100);
6 L_LOCAL_COLL V_COLL_DEMO ;
7 BEGIN
8 L_LOCAL_COLL := V_COLL_DEMO('Oracle 9i',
9 'Oracle 10g',
10 'Oracle 11g');
11 /*Use FOR loop to parse the array variable and print the elements*/
12 FOR I IN 1..L_LOCAL_COLL.COUNT
13 LOOP
14 DBMS_OUTPUT.PUT_LINE(I ||' '||'Printing Oracle version:' ||L_LOCAL_COLL(I));
15 END LOOP;
16 IF L_LOCAL_COLL.EXISTS(4) THEN
17 DBMS_OUTPUT.PUT_LINE('PRESENT');
18 ELSE
19 DBMS_OUTPUT.PUT_LINE('ABSENT');
20 END IF;
21 ---COUNT--
22 DBMS_OUTPUT.PUT_LINE('VALUE PRESENT IN '||L_LOCAL_COLL .COUNT);
23 DBMS_OUTPUT.PUT_LINE('VALUE OF LIMIT '||L_LOCAL_COLL .LIMIT);
24 DBMS_OUTPUT.PUT_LINE('FIRST VALUE '||L_LOCAL_COLL .FIRST||' '||L_LOCAL_COLL
(L_LOCAL_COLL .
25 DBMS_OUTPUT.PUT_LINE('LAST VALUE '||L_LOCAL_COLL .LAST||' '||L_LOCAL_COLL
(L_LOCAL_COLL .LAST
26 DBMS_OUTPUT.PUT_LINE('PRIOR VALUE OF LAST '||L_LOCAL_COLL .PRIOR(L_LOCAL_COLL
.LAST));
27 DBMS_OUTPUT.PUT_LINE('GIVE NULL VALUE '||L_LOCAL_COLL .NEXT(L_LOCAL_COLL .LAST));
28 L_LOCAL_COLL .EXTEND;
29 DBMS_OUTPUT.PUT_LINE('LAST VALUE AFTER EXTEND '||L_LOCAL_COLL .LAST );
30 IF L_LOCAL_COLL.EXISTS(L_LOCAL_COLL .LAST ) THEN
31 DBMS_OUTPUT.PUT_LINE(' LST VALUE PRESENT');
32 ELSE
33 DBMS_OUTPUT.PUT_LINE('ABSENT');
34 END IF;
35 DBMS_OUTPUT.PUT_LINE('VALUE PRESENT IN '||L_LOCAL_COLL .COUNT);
36 L_LOCAL_COLL := V_COLL_DEMO('ABC');
37 DBMS_OUTPUT.PUT_LINE('LAST VALUE AFTER EXTEND AND INTIALIZED '||L_LOCAL_COLL
(L_LOCAL_COLL .LA
38 DBMS_OUTPUT.PUT_LINE('VALUE PRESENT IN '||L_LOCAL_COLL .COUNT);
39 --L_LOCAL_COLL.DELETE(1);
40 DBMS_OUTPUT.PUT_LINE('LAST VALUE AFTER DELETE'||L_LOCAL_COLL .LAST );
41 DBMS_OUTPUT.PUT_LINE('LAST VALUE AFTER DELETE'||L_LOCAL_COLL .COUNT );
42* END;
SQL> /
1 Printing Oracle version:Oracle 9i
2 Printing Oracle version:Oracle 10g
3 Printing Oracle version:Oracle 11g
ABSENT
VALUE PRESENT IN 3
VALUE OF LIMIT 4
FIRST VALUE 1 Oracle 9i
LAST VALUE 3 Oracle 11g
PRIOR VALUE OF LAST 2
GIVE NULL VALUE
LAST VALUE AFTER EXTEND 4
LST VALUE PRESENT
VALUE PRESENT IN 4
LAST VALUE AFTER EXTEND AND INTIALIZED ABC
VALUE PRESENT IN 1
LAST VALUE AFTER DELETE1
LAST VALUE AFTER DELETE1

SQL> SELECT * FROM TAB_USE_NT_COL;


ID NUM
---------- --------------------------
1 NUM_NEST_T(10, 12, 3)
2 NUM_NEST_T(10, 12, 13)
Now, we will try to add an element (23) at the end of the collection instance for ID 2.
/*Enable the SERVEROUTPUT on to display the output*/
SET SERVEROUTPUT ON
/*Start the PL/SQL block*/
DECLARE
/*Declare a local variable of database collection type*/
L_INS_NUM NUM_NEST_T;
BEGIN
/*Select the collection instance into the local variable for ID = 2*/
SELECT num INTO L_INS_NUM
FROM tab_use_nt_col WHERE id=2;
/*Extend the collection variable using EXTEND method. Include the new
element at the end of the collection*/
L_INS_NUM.EXTEND;
L_INS_NUM (L_INS_NUM.LAST) := 23;
/*Update the local collection instance in the table for ID = 2*/
UPDATE tab_use_nt_col
SET num = L_INS_NUM
WHERE id=2;
END;
/
PL/SQL procedure successfully completed

You might also like