首页 / 数据库 / MySQL / Oracle PL/SQL复合数据类型
复合数据类型大致可以分为两类。一类是记录类型,适用于处理单行多列数据,有点类似于java中的VO;一类是集合类型,适用于处理单列多行的数据,类似java中的List,以下实验在Oracle 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可以被存储在数据库中。Oracle--plsql复合数据类型 http://www.linuxidc.com/Linux/2012-03/57482.htm--------------------------------------分割线 --------------------------------------rlwrap - 解决Linux下SQLPLUS退格、上翻键乱码问题 http://www.linuxidc.com/Linux/2013-07/87890.htmSQLPLUS spool 到动态日志文件名 http://www.linuxidc.com/Linux/2013-03/80988.htmOracle SQLPLUS提示符设置 http://www.linuxidc.com/Linux/2012-10/73326.htm通过设置SQLPLUS ARRAYSIZE(行预取)加快SQL返回速度 http://www.linuxidc.com/Linux/2011-07/38711.htmPL/SQL Developer实用技巧分享 http://www.linuxidc.com/Linux/2014-09/107391.htm--------------------------------------分割线 --------------------------------------更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12本文永久更新链接地址