Oracle自定义类型可以通过type/create type来声明或者创建
一,四种创建方式
1.1,使用create type创建object类型
create or replace type obj_type as object(id number,name varchar2(50 byte),birthday date);
1.2,使用create type创建table类型
create or replace type table_type as table of obj_type;
创建table类型要依赖于object类型,就是as table of后面接object类型。
1.3,使用type创建object类型
declaretype obj_type is record(id number,name varchar2(50 byte),birthday date);
begindbms_output.put_line('');
end;
1.4,使用type创建table类型
declaretype obj_type is record(id number,name varchar2(50 byte),birthday date);type table_type is table of obj_type;
begindbms_output.put_line('');
end;
区别是create可以独立执行,create后面用as
type只能在语句块中执行,type后面用is
二,Oracle数组类型或者说list类型
oracle有这样的一种方式,可以动态的将object变量添加到一个table变量中
create or replace type obj_type as object(id number,name varchar2(50 byte),birthday date);create or replace type table_type as table of obj_type;declarev_table table_type:=table_type();
beginv_table.extend;v_table(1):=obj_type(1,'lipiao',to_date('19970807','yyyymmdd'));v_table.extend;v_table(2):=obj_type(2,'cc',to_date('19951018','yyyymmdd'));for item in (select * from table(v_table)) loopdbms_output.put_line('id:'||item.id||',name:'||item.name||',birthday:'||item.birthday);end loop;
end;
这种方式需要每次先extend方法扩展一个object,然后给object赋值。
这种方法可以将table变量转换成游标。
下面这种方式可以自动扩展object,在19c版本测试可以使用,11g不行
declare
type obj_type is record(student_id number,student_name varchar2(20 byte),student_birthday date);
type table_type is table of obj_type index by binary_integer;
student_table table_type:=table_type();
student obj_type;
i number;
beginstudent_table.student_id:=100;student_table.student_name:='lipiao';student_table.student_birthday:=to_date('19970807','yyyymmdd');
student_table(1):=student;
student_table.student_id:=200;student_table.student_name:='cc';student_table.student_birthday:=to_date('19951018','yyyymmdd');
student_table(2):=student;i:=student_talbe.first;
loopexit when i is null;dmbs_output.put_line(student_table(i).student_name);i:=student_table.next(i);
end loop;
end;