Compare user-defined type : Comparable « Object Oriented Database « Oracle PL / SQL






Compare user-defined type

 
SQL>
SQL> create or replace
  2  type person as object(
  3   first_name varchar2(100),
  4   last_name varchar2(100) )
  5  /

Type created.

SQL>
SQL> alter type person
  2  add attribute dob date
  3  cascade not including table data
  4  /

Type altered.

SQL>
SQL> create or replace
  2  type employee as object(
  3   name person,
  4   empno number,
  5   hiredate date,
  6   sal number,
  7   commission number,
  8   order member function match ( p_employee employee ) return integer )
  9  /

Type created.

SQL>
SQL> create or replace
  2  type body employee as
  3   order member function match ( p_employee employee ) return integer is
  4   begin
  5    if self.empno > p_employee.empno then
  6      return 1;
  7    elsif self.empno < p_employee.empno then
  8      return -1;
  9    else
 10      return 0;
 11    end if;
 12   end;
 13  end;
 14  /

Type body created.

SQL>
SQL>
SQL> declare
  2   emp1 employee;
  3   emp2 employee;
  4  begin
  5   emp1 := employee( null, 12345, '01-JAN-01', 100, 100 );
  6   emp2 := employee( null, 67890, '01-JAN-01', 100, 100 );
  7   if emp1 > emp2 then
  8     dbms_output.put_line( 'Employee 1 is greater' );
  9   end if;
 10   if emp1 < emp2 then
 11     dbms_output.put_line( 'Employee 2 is greater' );
 12   end if;
 13   if emp1 = emp2 then
 14     dbms_output.put_line( 'Employees are equal' );
 15   end if;
 16  end;
 17  /

PL/SQL procedure successfully completed.

SQL>
SQL> drop type employee;

Type dropped.

SQL>
SQL> drop type person;

Type dropped.

SQL>
SQL>

 








Related examples in the same category

1.Raise error in object memthod
2.Create user type with order
3.Define "Compare" method and use it in order by clause
4.Compare the appartment objects and display the results