map
(1)将同一类型的对象实例映射成(number,date,varchar),之后进行比较。
(2) map成员函数不接受形参;
order
(1)两个对象实例进行比较
(2)一个对象类型只能有一个map或order ,且不能同时有。
–创建对象(map)
create or replace type person_type4 as object(
name varchar2(20),
gender varchar2(2),
birthdate date,
map member function getage return varchar2
);
–创建对象body(map)
create or replace type body person_type4
is
map member function getage return varchar2
is
begin
return trunc((sysdate-birthdate)/365) ;
end;
end;
—创建对象表
create table employee_table4 (
eno number(11),
person person_type4,
sal number(11),
job varchar2(20)
);
–插入数据
insert into employee_table4
values(001,person_type4(‘柳叶’,’男’,to_date(‘1996-10-20’,’yyyy-MM-dd’)),5000,’技师’);
insert into employee_table4
values(0021,person_type4(‘王瑶’,’女’,to_date(‘1966-10-20’,’yyyy-MM-dd’)),9000,’技师’);
insert into employee_table4
values(003,person_type4(‘胡斐’,’男’,to_date(‘1951-10-30’,’yyyy-MM-dd’)),15000,’教师’);
insert into employee_table4
values(004,person_type4(‘苗人凤’,’男’,to_date(‘1926-10-20’,’yyyy-MM-dd’)),50000,’大手’);
–查询数据
select * from employee_table4;
–比较年龄
declare
v_age varchar2(4);
type v_person_type is table of person_type4 ;–类型为对象的嵌套表
v_person_table v_person_type;
begin
select person bulk collect into v_person_table from employee_table4 ;
if v_person_table(1).getage() > v_person_table(2).getage() then
v_age := v_person_table(1).getage();
else
v_age := v_person_table(2).getage();
end if;
dbms_output.put_line(v_age);
end;
– order 方法的对象
create or replace type person_type5 as object(
name varchar2(20),
gender varchar2(2),
birthdate date,
order member function compare(p person_type5) return int
);
create or replace type body person_type5
is
order member function compare(p person_type5) return int
is
begin
case
when birthdate > p.birthdate then return 1;
when birthdate = p.birthdate then return 0;
when birthdate < p.birthdate then return -1;
end case;
end;
end;
—创建对象表
create table employee_table5 (
eno number(11),
person person_type5,
sal number(11),
job varchar2(20)
);
–插入数据
insert into employee_table5
values(001,person_type5(‘柳叶’,’男’,to_date(‘1996-10-20’,’yyyy-MM-dd’)),5000,’技师’);
insert into employee_table5
values(0021,person_type5(‘王瑶’,’女’,to_date(‘1966-10-20’,’yyyy-MM-dd’)),9000,’技师’);
select * from employee_table5;
–找出大年龄
declare
v_tem int;
type v_person_type is table of person_type5 ;–类型为对象的嵌套表
v_person_table v_person_type;
begin
select person bulk collect into v_person_table from employee_table5 ;
v_tem:= v_person_table(1).compare(v_person_table(2));
dbms_output.put_line(v_tem);
end;