首页 > Linux操作系统 > Linux操作系统 > 【笔记】oracle 数组实现
--一维数组:
--嵌套表
--尺寸没有限制。
--本质上是无序的
--VARRAY
--尺寸必须固定,所有的实例尺寸相同。
--在过程化语言中可以作为有序数组进行检索但在Oracle内部看成单个不能分割的单元。
--存储效率高。
--多维数组
--利用record 和record of
--建立测试表
drop table t_test_1;
create table t_test_1(
pid number(10),
pname varchar2(20),
birth date,
score number(3),
note varchar2(50)
);
--初始化,插入数据
--利用了nested table 来实现一维数组
--不需要制定上下限,下表可以不连续,需初始化
--
declare
type type_test_pid is table of t_test_1.pid%type
index by binary_integer;
type type_test_pname is table of t_test_1.pname%type
index by binary_integer;
type type_test_birth is table of t_test_1.birth%type
index by binary_integer;
type type_test_score is table of t_test_1.score%type
index by binary_integer;
type type_test_note is table of t_test_1.note%type
index by binary_integer;
my_test_pid type_test_pid;
my_test_panme type_test_pname;
my_test_birth type_test_birth;
my_test_score type_test_score;
my_test_note type_test_note;
i number(10) := 0;
begin
for i in 1 .. 10000
loop
my_test_pid(i) := i;
my_test_panme(i) := 'names_' || to_char(i);
my_test_birth(i) := sysdate;
my_test_score(i) := to_number(nvl(substr(to_char(i), -2), to_char(i)));
my_test_note(i) := my_test_panme(i)|| ' score is ' ||
nvl(substr(to_char(i), -2), to_char(i));
end loop;
forall i in 1 .. 10000
insert into t_test_1
values(
my_test_pid(i),
my_test_panme(i),
my_test_birth(i),
my_test_score(i),
my_test_note(i)
);
commit;
exception
when others then
rollback;
end;
--使用varray实现数组
--自定义一个TYPE使用VARRAY来得到一个数组
--但只能对基本类型定义
--此种方式实现的数组,需要制定上限,且下标连续,在使用之前须初始化
--适用于数组长度不太大的情况
declare
type num_pid is varray(1000) of t_test_1.pid%type;
v_num_pid num_pid;
i number(8):=1;
begin
v_num_pid := num_pid();
v_num_pid.extend;
v_num_pid(1) := 1;
v_num_pid.EXTEND(999,1);--一次性添加999个第1个元素的副本
for i in 1 .. 1000
loop
--v_num_pid.extend; --每次扩展一个
v_num_pid(i) := i;
execute immediate 'update t_test_1 set score = trunc(score/2)
where pid=:1 '
using v_num_pid(i);
end loop;
end;
--使用record和table实现多维数组
--Index-by-tables,nested table,varray的区别
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/16179598/viewspace-539648/,如需转载,请注明出处,否则将追究法律责任。