03 December 2006

On Associative Arrays, Nested Tables and Varrays In Oracle

Oracle has three different types of collections. I mentioned them in one of my previus posts. You can take more information there.
Now, i want to give examples how to use them:


SQL> DECLARE
  2    TYPE occupation_table IS TABLE OF VARCHAR2(64) INDEX BY VARCHAR2(16);
  3    occupations occupation_table;
  4    k           VARCHAR2(16);
  5  BEGIN
  6    occupations('One') := 'Architecture';
  7    occupations('Two') := 'Engineer';
  8    k := occupations.FIRST;
  9    WHILE k IS NOT NULL LOOP
 10      dbms_output.put_line('occupations(''' || k || ''') is ' || occupations(k));
 11      k := occupations.NEXT(k);
 12    END LOOP;
 13  END;
 14  /

occupations('One') is Architecture
occupations('Two') is Engineer

PL/SQL procedure successfully completed
SQL>

2. Varrays
SQL> create type t_telephone_numbers is varray(2) of varchar2(10);
Type created
SQL> create table workers( name varchar2(16), telephone_list t_telephone_numbers );
Table created
SQL> INSERT INTO workers VALUES('Mennan',t_telephone_numbers('2122122122', '2122221133') );
1 row inserted
SQL> SELECT * FROM workers;
NAME             TELEPHONE_LIST
---------------- --------------
Mennan          
[object]
SQL> SELECT * FROM table(SELECT telephone_list FROM workers WHERE name = 'Mennan'  );
COLUMN_VALUE
------------
2122122122
2122221133

SQL>

3. Nested Tables
SQL> create type t_hobbies is table of varchar2(16);
  2  /

Type created
SQL> create table workers( name varchar2(16), hobbies t_hobbies )
  2    nested table hobbies store as workers_hobbies;

Table created
SQL> SELECT table_name, nested FROM user_tables WHERE table_name like 'WORKERS%';
TABLE_NAME                     NESTED
------------------------------ ------
WORKERS                        NO
WORKERS_HOBBIES                YES

SQL> INSERT INTO workers VALUES('Mennan',t_hobbies('Reading Book', 'Listening') );
1 row inserted
SQL> INSERT INTO workers VALUES('Ali',t_hobbies('Swimming', 'Coding', 'Football') );
1 row inserted
SQL> commit;
Commit complete
SQL> SELECT * FROM workers;
NAME             HOBBIES
---------------- -------
Mennan           [object]
Ali             
[object]
SQL> SELECT * FROM workers_hobbies;
SELECT * FROM workers_hobbies
ORA-22812: cannot reference nested table column's storage table
SQL> SELECT * FROM table(SELECT hobbies FROM workers WHERE name = 'Mennan'  );
COLUMN_VALUE
----------------
Reading Book
Listening

SQL> INSERT INTO workers VALUES('Ayse',t_hobbies('Reading Horror Books') );
INSERT INTO workers VALUES('Ayse',t_hobbies('Reading Horror Books') )
ORA-12899: value too large for column "HR"."WORKERS_HOBBIES"."COLUMN_VALUE" (actual: 20, maximum: 16)
SQL> alter type t_hobbies modify element type varchar2(8) cascade;
alter type t_hobbies modify element type varchar2(8) cascade
ORA-22324: altered type has compilation errors
ORA-22328: object "HR"."T_HOBBIES" has errors.
PLS-00729: only widening of the collection element type is allowed
ORA-06550: line 0, column 0:
PL/SQL: Compilation unit analysis terminated

SQL> alter type t_hobbies modify element type varchar2(64) cascade;
Type altered
SQL> INSERT INTO workers VALUES('Ayse',t_hobbies('Reading Horror Books') );
1 row inserted
SQL> commit;
Commit complete
SQL> SELECT * FROM table(SELECT hobbies FROM workers WHERE name = 'Ayse'  );
COLUMN_VALUE
----------------------------------------------------------------
Reading Horror Books

SQL>

No comments: