可變數(shù)組,是一種集合。一個可變數(shù)組是一個對象的集合,其中每個對象都具有相同的數(shù)據類型??勺償?shù)組的大小在創(chuàng)建時決定。在表中創(chuàng)建可變數(shù)組后,可變數(shù)組在主表中即為一個列。
可變數(shù)組特性
1)可變數(shù)組主要的特性即是元素的最大個數(shù)是有限制
2)可變數(shù)組下標固定為1,上限可以擴展
3)在可變數(shù)組聲明時自動設置為NULL值.所謂的空值指的是集合本身是空,不是針對它所擁有的元素,故在元素引用前需要對其進行初始化
可變數(shù)組創(chuàng)建語法:
CREATE [OR REPLACE] TYPE 類型名稱 IS VARRAY(長度) OF 數(shù)據類型;
例如:定義一個可變數(shù)組類型 my_varray ,它的最大容量是5,元素類型是 VARCHAR2.
create or replace type my_varray as varray(5) of varchar2(50);
1、簡單類型的可變數(shù)組
declare
type weeks is varray(12) of varchar2(50);
my_week weeks:=weeks('星期一','星期二','星期三','星期四','星期五'); -- 定義一個類型為 weeks 的可變數(shù)組變量 my_week , 此時 my_week 是一個有5個元素的數(shù)組
begin
dbms_output.put_line('第三個元素為:'||my_week(3));
dbms_output.put_line('第一個元素為:'||my_week.first);
dbms_output.put_line('第二個元素:'||my_week.next(my_week.first));
dbms_output.put_line('倒數(shù)第二個元素:'||my_week.prior(my_week.last));
dbms_output.put_line('最大返回的元素個數(shù):'||my_week.limit);--最大返回的元素個數(shù)
my_week.extend(1);--集合擴充1個長度
my_week(6):='星期六';
my_week.extend(4,2); --集合擴充2個長度,使用原始集合的第2個數(shù)據填充
my_week.trim(); --刪除末尾的 1 個元素
my_week.trim(2); -- 刪除末尾的 2 個元素
dbms_output.put_line('最后一個元素為:'||my_week.last);
dbms_output.put_line('總共有:'||my_week.count);
if my_week.exists(4) then
dbms_output.put_line('第四個元素存在');
end if;
if not my_week.exists(10)then
dbms_output.put_line('第十個元素不存在');
end if;
for i in my_week.first..my_week.last loop
dbms_output.put_line('元素:'||i||'值 為:'||my_week(i));
end loop;
my_week.delete(); -- 刪除所有元素
end;
2、定義TYPE類型的可變數(shù)組
declare
type company_type is varray(20) of emb.company.company_code%TYPE;
v_companycode company_type:=company_type('1111111');
begin
select company_code into v_companycode(1) from emb.company
where company_name = '廣東分公司';
dbms_output.put_line('公司code:'||v_companycode(1));
end;
輸出結果:公司code:204
3、定義二維可變數(shù)組
declare
--一維數(shù)組
type first_varray_type is varray(10) of int;
--二維數(shù)組
type second_varray_type is varray(10) of first_varray_type;
--初始化
variable_varray second_varray_type:=second_varray_type(
first_varray_type(34,23,53,34),
first_varray_type(23,67,95),
first_varray_type(9,4)
);
begin
dbms_output.put_line('顯示二維數(shù)組所有元素:');
for i in 1..variable_varray.count loop
for j in 1..variable_varray(i).count loop
dbms_output.put_line('variable_varray('||i||','||j||')='||variable_varray(i)(j));
end loop;
end loop;
end;
輸出結果:
顯示二維數(shù)組所有元素:
variable_varray(1,1)=34
variable_varray(1,2)=23
variable_varray(1,3)=53
variable_varray(1,4)=34
variable_varray(2,1)=23
variable_varray(2,2)=67
variable_varray(2,3)=95
variable_varray(3,1)=9
variable_varray(3,2)=4
4、定義復合類型的可變數(shù)組
CREATE OR REPLACE TYPE cla_type AS OBJECT(
v_claim_accidentno varchar2(50),
v_claim_version number,
v_claim_validflag number
);
/
declare
type claim_varray is varray(3) of cla_type;
v_claim_varray claim_varray:= claim_varray(cla_type('AAA',1,1),cla_type('BBB',2,1),cla_type('CCC',3,0));
begin
for i in v_claim_varray.first..v_claim_varray.last loop
DBMS_OUTPUT.put_line('事故號:'||v_claim_varray(i).v_claim_accidentno||' 版本號:'||v_claim_varray(i).v_claim_version||' 有效標識:'||v_claim_varray(i).v_claim_validflag);
end loop;
end;
/
輸出結果:
事故號:AAA 版本號:1 有效標識:1
事故號:BBB 版本號:2 有效標識:1
事故號:CCC 版本號:3 有效標識:0