當(dāng)前位置:
首頁 >
Oracle自定义类型
發(fā)布時(shí)間:2025/3/8
33
豆豆
生活随笔
收集整理的這篇文章主要介紹了
Oracle自定义类型
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Oracle自定義類型可以通過type/create type來聲明或者創(chuàng)建
一,四種創(chuàng)建方式
1.1,使用create type創(chuàng)建object類型
create or replace type obj_type as object(id number,name varchar2(50 byte),birthday date);1.2,使用create type創(chuàng)建table類型
create or replace type table_type as table of obj_type;創(chuàng)建table類型要依賴于object類型,就是as table of后面接object類型。
1.3,使用type創(chuàng)建object類型
declaretype obj_type is record(id number,name varchar2(50 byte),birthday date); begindbms_output.put_line(''); end;1.4,使用type創(chuàng)建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;區(qū)別是create可以獨(dú)立執(zhí)行,create后面用as
type只能在語句塊中執(zhí)行,type后面用is
二,Oracle數(shù)組類型或者說list類型
oracle有這樣的一種方式,可以動(dòng)態(tài)的將object變量添加到一個(gè)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方法擴(kuò)展一個(gè)object,然后給object賦值。
這種方法可以將table變量轉(zhuǎn)換成游標(biāo)。
?
下面這種方式可以自動(dòng)擴(kuò)展object,在19c版本測(cè)試可以使用,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;?
總結(jié)
以上是生活随笔為你收集整理的Oracle自定义类型的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【JAVA基础篇】反射
- 下一篇: 工程图标注粗糙度_Inventor教程之