Creating and Using VARRAYs : varray « PL SQL « Oracle PL / SQL






Creating and Using VARRAYs

    
SQL>
SQL> create type employee_type as object (
  2    employee_id       number,
  3    first_name        varchar2(30),
  4    last_name         varchar2(30)
  5  );
  6  /

Type created.

SQL>
SQL> create type employee_list_type as varray(50) of employee_type;
  2  /

Type created.

SQL> create table departments (
  2    department_id   number,
  3    department_name varchar2(30),
  4    manager         employee_type,
  5    employees       employee_list_type );

Table created.

SQL>
SQL> insert into departments ( department_id,
  2                            department_name,
  3                            manager,
  4                            employees )
  5  values ( 10,
  6           'Accounting',
  7           employee_type( 1, 'Danielle', 'Steeger' ),
  8           employee_list_type(
  9             employee_type( 2, 'Madison', 'Sis' ),
 10             employee_type( 3, 'Robert', 'Cabove' ),
 11             employee_type( 4, 'Michelle', 'Sechrist' ))
 12  );

1 row created.

SQL>
SQL>
SQL> insert into departments ( department_id,
  2                            department_name,
  3                            manager,
  4                            employees )
  5  values ( 20,
  6           'Research',
  7           employee_type( 11, 'Ricky', 'Lil' ),
  8           employee_list_type(
  9             employee_type( 12, 'Ricky', 'Ricardo' ),
 10             employee_type( 13, 'Lucy', 'Ricardo' ),
 11             employee_type( 14, 'Fred', 'Mertz' ),
 12             employee_type( 15, 'Ethel', 'Mertz' ))
 13  );

1 row created.

SQL>
SQL> column department_name format a13
SQL> column employees format a63 word_wrapped
SQL> select department_name, employees
  2    from departments;

DEPARTMENT_NA EMPLOYEES(EMPLOYEE_ID, FIRST_NAME, LAST_NAME)
------------- ---------------------------------------------------------------
Accounting    EMPLOYEE_LIST_TYPE(EMPLOYEE_TYPE(2, 'Madison', 'Sis'),
              EMPLOYEE_TYPE(3, 'Robert', 'Cabove'), EMPLOYEE_TYPE(4,
              'Michelle', 'Sechrist'))

Research      EMPLOYEE_LIST_TYPE(EMPLOYEE_TYPE(12, 'Ricky', 'Ricardo'),
              EMPLOYEE_TYPE(13, 'Lucy', 'Ricardo'), EMPLOYEE_TYPE(14, 'Fred',
              'Mertz'), EMPLOYEE_TYPE(15, 'Ethel', 'Mertz'))


SQL>
SQL>
SQL> drop table departments;

Table dropped.

SQL>
SQL> drop type employee_list_type;

Type dropped.

SQL>
SQL> drop type employee_type;

Type dropped.

SQL>
SQL>

   
    
    
  








Related examples in the same category

1.Varray constructors.
2.legal and illegal varray assignments.
3.TYPE Strings IS VARRAY(5) OF VARCHAR2(10)
4.Create a varray based on user defined type
5.Store pre-defined constants in VARRAY
6.Query a stored varray.
7.VARRAY of VARCHAR2 and Varray of number
8.Table of numbers and varray of numbers
9.Initialize the array and create two entries using the constructor
10.Reference elements in varray
11.assignments to varray elements, and the ORA-6532 and ORA-6533 errors.
12.ORA-06533: Subscript beyond count
13.Compare two varray variables
14.Constructs a two varrays and one nested table type in the database
15.Control varray index with if statement
16.Create type prices with a varray of number
17.Declare the varray with null values.
18.Declaring a VARRAY of scalar variable
19.Define a varray of integer with 3 rows
20.Define a varray of twelve strings.
21.Define a varray with a null element constructor and extends it one element at a time by a formula
22.Define a varray with a null element constructor and extends it one element at a time.
23.Define a varray with a three element constructor of null elements and attempt to populate it beyond three elements.
24.Define a varray with a three element constructor of null elements.
25.Hard code value in varray and use for loop to insert them to a table
26.Nested varray
27.Reference varray.count in for loop
28.Store 12 months in varray of string
29.Use table() function to display varray type column
30.exceeded maximum VARRAY limit
31.Assign values to subscripted members of the varray.
32.Check the size of a varray
33.Declare an array initialized as a no-element collection.
34.Extend with null element to the maximum limit size.
35.Initialization and assignment with a numeric index value to an associative array.
36.Initialization and assignment with a unique string index value to an associative array.
37.Associative array example
38.Assigns a value to the indexed value.
39.Avoid traversing an associative array where no elements are initialized.
40.Subscript index values begin at 1, not 0