Oracle PL/SQL复合数据类型,oraclepl
复合数据类型大致可以分为两类。一类是记录类型,适用于处理单行多列数据,有点类似于java中的VO;一类是集合类型,适用于处理单列多行的数据,类似java中的List,以下实验在11.2.0.1.0版本下做的。
1.记录类型
drop table test purge;
create table test(
id number(2),
name varchar2(60)
);
insert into test values(1,'aaa');
insert into test values(2,'bbb');
insert into test values(3,'ccc');
insert into test values(4,'ddd');
insert into test values(5,'eee');
commit;
--显式定义记录类型
declare
type t_record is record
(
id test.id%type,
name test.name%type
);
var_record t_record;
coun number := 0;
begin
for c_row in (select id,name from test) loop
coun :=
coun + 1;
dbms_output.put_line('第'||coun||'循环');
var_record.id := c_row.id;
var_record.name := c_row.name;
dbms_output.put_line('记录:'||var_record.id||'---'||var_record.name);
dbms_output.put_line('游标:'||c_row.id||'---'||c_row.name);
end loop;
exception when others then
dbms_output.put_line(sqlcode||sqlerrm);
end;
/
输出结果:
第1循环
记录:1---aaa
游标:1---aaa
第2循环
记录:2---bbb
游标:2---bbb
第3循环
记录:3---ccc
游标:3---ccc
第4循环
记录:4---ddd
游标:4---ddd
第5循环
记录:5---eee
游标:5---eee
--隐式定义记录类型
declare
t_record1 test%rowtype;
cursor c_row(v_id in varchar2) is select id,name from test where id <= v_id;
t_record2 c_row%rowtype;
begin
for row_test in c_row(3) loop
t_record1.id := row_test.id;
t_record1.name := row_test.name;
t_record2.id := row_test.id;
t_record2.name := row_test.name;
dbms_output.put_line('表的rowtype:'||t_record1.id||'---'||t_record1.name);
dbms_output.put_line('游标的rowtype:'||t_record2.id||'---'||t_record2.name);
dbms_output.put_line('游标:'||row_test.id||'---'||row_test.name);
end loop;
exception when others then
dbms_output.put_line(sqlcode||sqlerrm);
end;
/
输出结果:
表的rowtype:1---aaa
游标的rowtype:1---aaa
游标:1---aaa
表的rowtype:2---bbb
游标的rowtype:2---bbb
游标:2---bbb
表的rowtype:3---ccc
游标的rowtype:3---ccc
游标:3---ccc
如果在显式和隐式定义记录中选择,我倾向于选择显式的定义,因为让逻辑更加清晰。
2.集合类型
--索引表
declare
cursor cur_test is select id,name from test;
type t_test1 is table of test%rowtype index by binary_integer;
var_test1 t_test1;
begin
SELECT id,name INTO var_test1(0) FROM test WHERE id=1;
dbms_output.put_line('var_test1(0):'||var_test1(0).id||'---'||var_test1(0).name);
SELECT id,name INTO var_test1(10) FROM test WHERE id=2;
dbms_output.put_line('var_test1(10):'||var_test1(10).id||'---'||var_test1(10).name);
end;
var_test1(0):1---aaa
var_test1(10):2---bbb
--嵌套表
DECLARE
TYPE t_test1 IS TABLE OF test.id%TYPE;
var_test1 t_test1;
begin
var_test1 := t_test1(1,2,3);
dbms_output.put_line('var_test1: '||var_test1(1)||','||var_test1(2)||','||var_test1(3));
end;
var_test1: 1,2,3
--varray表
DECLARE
TYPE t_test1 IS VARRAY (10) OF test.id%TYPE;
var_test1 t_test1;
begin
var_test1 := t_test1(1,2,3);
dbms_output.put_line('var_test1: '||var_test1(1)||','||var_test1(2)||','||var_test1(3));
end;
var_test1: 1,2,3
索引表和嵌套表集合中的元素的数量没有限制,varray集合中是没有限制的。
索引表不能存储在数据库中,嵌套表和varray可以被存储在数据库中。
定义并使用变量
PL/SQL有四种类型:标量类型,复合类型,引用类型 (reference),LOB(Large Obejct)类型
一、标量类型
最常用的就是标量类型,是指只能存放单个数值的变量,包括数字类型、字符类型、日期类型和布尔类型,每种类型又包含相应的子类型。
常量标量类型如下:
VARCHAR2 (n) , CHAR (n), NUMBER (p,s),DATE, TIMESTAMP , LONG , LONG RAW ,BOOLEAN,BINARY_INTEGER(仅 PL / SQL使用),BINARY_FLOAT和BINARY_DOUBLE(10g新引入的)
定义标量:
identifier [CONSTANT] datatype [NOT NULL] [:=| DEFAULT expr]
使用标量需要注意的是=号被:=取代,与delphi一样的赋值符号@_@
例子:
v_name VARCHAR2 ( 10 );
v_rate CONSTANTS NUMBER ( 4 , 2 ) : = 3.04 ;
为了防止定义的变量类型与表中的字段类型不一致,可以使用%TYPE来定义:
v_name employee.name % TYPE;
如上面所示,v_name的类型就与表 employee中的name字段类型一样!!
二、复合变量:
用于存放多个值的变量称为复合变量,包括PL/SQL记录,PL/SQL表,嵌套表和VARRAY四种类型
1.PL/SQL记录
类似于C/C++中的结构概念:
declare
TYPE employee_record is RECORD(
id employee.id % TYPE,
name employee.name % TYPE,
email employee.email % TYPE);
em_record employee_record;
begin
select id,name,email into em_record from employee where name =& name;
dbms_output.put_line( ' 雇员名: ' || em_record.name || ' 雇员ID: ' || em_record.id);
end ;
2.PL/SQL表,类似于数组概念,不同的是PL/SQL表允许负值下标,而且没有上下限,如:
declare
TYPE employee_table is table of employee.name % TYPE index by BINaRY_INTEGER;
em_table employee_table;
begin
select name into em_table( - 1 ) from employee where name =& name;
dbms_output.put_line( ' 雇员名: ' || em_table( - 1 ));
end ;
3.嵌套表,与PL/SQL 表相似,不同的是嵌套表可以做表列的数据类型,而PL/SQL表不能,使用嵌套表作为表列时,必须为其指定专门的存储表,如:
create ......余下全文>>
binary_double 双精度64位
binary_float 双精度32位
blob,clob,nclob 三种大型对象(LOB),用来保存较大的图形文件或带格式的文本文件,如Miceosoft Word文档,以及音频、视频等非文本文件,最大长度是4GB。LOB有几种类型,取决于你使用的字节的类型,Oracle 8i实实在在地将这些数据存储在数据库内部保存。
可以执行读取、存储、写入等特殊操作。
interval day to second,interval year to month 类型存储两个TIMESTAMP之间的时间差异,秒,月
long 可变长字符列,最大长度限制是2GB,用于不需要作字符串搜索的长串数据,如果要进行字符搜索就要用varchar2类型。
timestamp 时间戳,比date更精确
timestamp with time zone ,timestamp with local time zone 时间戳(时区不一样)
number 数字型
回答完毕
参考资料:dev.csdn.net/article/38/38823.shtm