Call object constructor in an insert statement : Constructor « Object Oriented Database « Oracle PL / SQL






Call object constructor in an insert statement

   
SQL>
SQL>
SQL> create or replace
  2  type person as object (
  3   first_name varchar2(100),
  4   last_name varchar2(100),
  5   dob date,
  6   phone varchar2(100),
  7   member function get_last_name return varchar2,
  8   member function get_phone_number return varchar2 )
  9  not final
 10  /

Type created.

SQL>
SQL>
SQL> create or replace
  2  type body person as
  3    member function get_last_name return varchar2 is
  4    begin
  5      return self.last_name;
  6    end;
  7    member function get_phone_number return varchar2 is
  8    begin
  9      return self.phone;
 10    end;
 11  end;
 12  /

Type body created.

SQL>
SQL> create or replace
  2  type new_employee under person (
  3    empno number,
  4    hiredate date,
  5    work_phone varchar2(100),
  6    overriding member function get_phone_number return varchar2,
  7    member function get_home_phone_number return varchar2 )
  8  not final
  9  /

Type created.

SQL>
SQL> create or replace
  2  type body new_employee as
  3    overriding member function get_phone_number return varchar2 is
  4    begin
  5      return self.work_phone;
  6    end;
  7    member function get_home_phone_number return varchar2 is
  8    begin
  9      return self.phone;
 10    end;
 11  end;
 12  /

Type body created.

SQL>
SQL>
SQL>
SQL>
SQL> create table person_table( p person );

Table created.

SQL>
SQL>
SQL>
SQL> insert into person_table values
  2  ( person( null, 'Kyte', null, '703.555.5555' ) )
  3  /

1 row created.

SQL>
SQL> insert into person_table values
  2  ( new_employee(  null, 'Beck', null, '703.555.1111', 1234, null, '703.555.2222' ) )
  3  /

1 row created.

SQL>
SQL> insert into person_table values
  2  ( new_employee(  null, 'Viper', null, '703.555.3333', 5678, null, '703.555.4444' ) )
  3  /

1 row created.

SQL>
SQL> select * from person_table;


P(FIRST_NAME, LAST_NAME, DOB, PHONE)
--------------------------------------------------------------------------------
person(NULL, 'Kyte', NULL, '703.555.5555')
NEW_EMPLOYEE(NULL, 'Beck', NULL, '703.555.1111', 1234, NULL, '703.555.2222')
NEW_EMPLOYEE(NULL, 'Viper', NULL, '703.555.3333', 5678, NULL, '703.555.4444')

SQL> drop table person_table;

Table dropped.

SQL>
SQL> drop type new_employee;

Type dropped.

SQL>
SQL> drop type person;

Type dropped.

SQL>
SQL>

   
    
  








Related examples in the same category

1.Insert value with constructor method
2.Use type Constructor to insert data to object table
3.Use constructor to create new objects
4.This script builds a sample object type with constructor.
5.This script demonstrates the user-defined constructor method.
6.Demonstrates object initialization.