Document directory
Http://www.smart-soft.co.uk/Oracle/oracle-plsql-tutorial-part-11.htmPLSQL (Varrays)
Varrays are one-dimenys, variable length arrays, with the maximum length defined in the declaration. these are used when it is known in advance that you will only need a few of the items to be stored and you know in advance the maximum number that you will need to hold.
A good example of the use of these wocould be for storing telephone numbers or addresses for say employees-each person is unlikely to have more than half a dozen telephone numbers or addresses. we can create a table for storing these as follows :-
Type phone_no_tab_typ is varray (6) of varchar2 (20;
Phone_nos phone_no_tab_typ;
The preceding 2 statements have created an un-initialised array that is available within our PL/SQL session. inserting and retrieving data is done in the same way as with nested tables. first of all we have to initialise the array, then we can insert data into and retrieve data from the varray.
As with nested tables, if you attempt to read from or write to an un-initialised varray in PLSQL, Oracle will generate an error which will cause an exception to be raised, as in the following examples:
SQL> declare
2 type phone_no_tab is varray (6) of varchar2 (20 );
3 phone_nos phone_no_tab;
4 begin
5 dbms_output.put_line ('phone _ NO (1) is '| phone_nos (1 ));
6 end;
7/
Declare
*
Error at line 1:
ORA-06531: Reference to uninitialized collection
ORA-06512: At line 5
SQL> declare
2 type phone_no_tab is varray (6) of varchar2 (20 );
3 phone_nos phone_no_tab;
4 begin
5 phone_nos (1): = 1;
6 end;
7/
Declare
*
Error at line 1:
ORA-06531: Reference to uninitialized collection
ORA-06512: At line 5
We can initialise our PLSQL varrays either in the Declaration or separately. if the varray is only partially initialised we have to use the extend method to define extra cells. let's see how to do this in the next example in our PLSQL tutorial.
SQL> declare
2 type phone_no_tab is varray (6) of varchar2 (20 );
3 phone_nos phone_no_tab: = phone_no_tab ('+ 44 (0) 117 942 2508'); -- only 1st cell initailised
4 begin
5 phone_nos (1): = '2017 0117 123 ';
6 dbms_output.put_line ('phone _ NO (1) is '| phone_nos (1 ));
7 END;
8/
Phone_no (1) is 21
This example partially initialised the table when it was declared. notice the use of the constructor for our collection on line 3 of our anonymous PLSQL block. the use of this enabled us to create one cell in the array. we we cocould have used the constructor to define as your elements as we liked, to do this we woshould just have to separate each element by a comma. for example to define two elements, the syntax wocould be like this:
Phone_nos phone_no_tab: = phone_no_tab ('+ 44 (0) 117 942 2508', '0117 942 2508 ');
This next example in our PLSQL tutorial will demonstrate the use of the EXTEND method.
SQL> DECLARE
2 TYPE phone_no_tab is varray (6) OF VARCHAR2 (20 );
3 phone_nos phone_no_tab;
4 BEGIN
5 phone_nos: = phone_no_tab (); -- create empty varray
6 phone_nos.EXTEND (2); -- create first 2 cells
7 phone_nos (1): = '2017 0117 123 ';
8 DBMS_OUTPUT.PUT_LINE ('phone _ no (1) is '| phone_nos (1 ));
9 END;
10/
Phone_no (1) is 0117 942 2508
Note the use of the constructor again on line 5, without this we wocould get the same error as before (ORA-06531 Reference to uninitialized collection ). this is because we created a PL/SQL varray with no elements, so we have to use the EXTEND method to create one or more NULL elements (in this example we created two ).
The EXTEND method works the same way as it does with PLSQL nested tables-if we don't specify the number of elements to create, then just one will be created. we can also optionally specify that each new element shocould be set to the same value as an existing element as in the following example in our PLSQL tutorial.
SQL> DECLARE
2 TYPE phone_no_tab is varray (6) OF VARCHAR2 (20 );
3 phone_nos phone_no_tab;
4 BEGIN
5 phone_nos: = phone_no_tab ();
6 phone_nos.EXTEND (2 );
7 phone_nos (1): = '2017 0117 123 ';
8 phone_nos.EXTEND (2, 1 );
9 DBMS_OUTPUT.PUT_LINE ('phone _ no (4) is '| phone_nos (4 ));
10 END;
11/
Phone_no (4) is 0117 942 2508
On line 8, we specified that we two new elements shocould be created and that each new element shocould have the same value as the first element (the 2nd parameter specifies the cell which shocould be copied ).
It is also worth noting that as with nested tables we have no control over where these elements are created, oracle automatically appends them to the existing table so that we always have a dense structure unless we specifically delete elements. however unlike nested tables, varrays have an upper limit, so if we attempt to add more rows than the upper limit this causes the error ORA-06532: Subscript outside of limit.
So far in this PLSQL tutorial we 've learned how to initialise the table and add new elements to it. the next example in our PLSQL tutorial shows what happens when we reference an element that doesn't exist after we 've ve initialised the table:
SQL> DECLARE
2 TYPE phone_no_tab is varray (6) OF VARCHAR2 (20 );
3 phone_nos phone_no_tab ();
4 BEGIN
5 phone_nos.EXTEND (2 );
6 DBMS_OUTPUT.PUT_LINE (
7 'phone _ no (4) is '| phone_nos (4 ));
8 END;
9/
Declare
*
ERROR at line 1:
ORA-06533: Subscript beyond count
ORA-06512: at line 6
As you can see Oracle raises an exception and we get the Oracle error ORA-06533.
Let's finish off this Oracle plsql tutorial by having a look at the other methods we can use on varrays in PL/SQL.
COUNT: This function returns the number of elements (cells) in the collection
Delete: This deletes all the elements in the collection as varrays cannot be sparse. after deleting the whole collection any subsequent attempt to read or write to an element in the collection gives the following error: "ORA-06533: subscript beyond count ".
Exists (n): used to determine if the specified element has been created and not deleted, returns true if the element exists, false if not
Trim (n): deletes the last n elements in the PLSQL collection, N defaults to 1. an Oracle error (ORA-06533: subscript beyond count) is generated if you attempt to trim elements that have already been trimmed or if the collection has been deleted.
The next PLSQL tutorial looks at PL/SQL associative arrays.