PLSQL集合型別

2022-01-26 06:40:53 字數 3159 閱讀 4103

plsql

集合型別

--聯合陣列(索引表)

/*用於儲存某個資料型別的資料集合型別

。通過索引獲得聯合陣列中得值

如下例子:

*/declare 

cursor cur_chars is select chars from a;--宣告游標

type str_type is table of a.chars%type--宣告聯合陣列

index by binary_integer; --宣告聯合陣列的索引

strs str_type;--宣告str_type 的聯合陣列strs

counts integer := 0;

begin 

for cur_chars_rec in cur_chars loop

counts := counts + 1;

strs(counts) := cur_chars_rec.chars;

dbms_output.put_line('counts('||counts||'):'||strs(counts));

end loop;

end;

這個聯合索引型別其實就是表資料的索引,定義游標然後把遍歷到的結果存放在聯合陣列中,就相當於程式的基本資料型別陣列

1.宣告游標用來遍歷表資料2.宣告聯合陣列用來儲存游標遍歷的資料集3.宣告索引

4.定義聯合陣列

聯合陣列名稱

聯合陣列type

strs(counts) := cur_chars_rec.chars;  聯合陣列賦值命令

--巢狀表

/*plsql表型別之一,他與聯合陣列具有相同的結構,都是使用下標訪問陣列

主要區別在於,巢狀表可以儲存在資料庫表的列中,而聯合陣列不行

*/declare

cursor my_cursor is select chars from a where num='ww' ;--定義游標

type table_type is table of a.chars%type;--宣告表型別

table_name table_type:=table_type();--定義並初始化巢狀表

counts integer := 1;

begin

for my_cursor_rec in my_cursor loop

counts := counts;

table_name.extend;--

表做成可擴充套件的

***table_name(counts):=my_cursor_rec.chars;

dbms_output.put_line('counts'||counts||' := '||table_name(counts));

end loop;

end;

--建立變長陣列

declare

type varray_type is array(10) of number(6);

varray_name varray_type := varray_type(); --變長陣列同巢狀表相同都需要初始化收集

counts integer := 0;

begin

for i in 1..5 loop

counts := counts + 1;

varray_name.extend;

varray_name(counts) := i;--這個都是相同的使用下標

dbms_output.put_line('counts('||counts||'):='||i);

end loop;

end;

varray_name.extend; 的使用超過範圍的時候需要使用擴充套件

集合函式的使用count 數量

first

第乙個

last

最後乙個

varrary_array.count varrary_array.first  varrary_array.last

varrary_array.trim(4) 從後往前刪除

4個陣列元素

多層集合

集合作為集合的元素

變長陣列為例

就相當於資料的巢狀

--多層集合(陣列的巢狀)

declare

type var_type1 is varray(5) of integer;--宣告變長陣列

type var_type2 is varray(3) of var_type1;--宣告多層集合,集合元素型別為var_type1

varray_integer var_type1 := var_type1(1,2,3,4,5);--集合給初始值

varray_multi var_type2 := var_type2(varray_integer);

begin

for i in 1..5 loop

dbms_output.put_line('varray_integer('||i||'):='||varray_integer(i));

end loop;

varray_multi.extend;

varray_multi(2) := var_type1(6,7,8,9,10);

varray_multi.extend;

varray_multi(3) := var_type1(5,4,3,2,1);

for i in 1..3 loop

for j in 1..5 loop

dbms_output.put_line('varray_multi['||i||']'||'['||j||']=='||varray_multi(i)(j));

end loop;

end loop;

end;

--集合的方法

delete --刪除集合元素

extend --為集合增加元素空間,實現空間的擴充套件以填充新元素。

count --集合元素中的數量

exists --如果指定的元素在集合中存在,則返回true

delete --刪除指定集合位置的元素

first and  last --集合中的第乙個和最後乙個元素

prior and next --返回集合指定位置的前乙個和後乙個元素

trim --從集合尾部刪除元素

limit --返回集合允許元素的最大數量

PL SQL 集合型別

建立乙個型別 create or replace type project is object project no number 2 title varchar2 35 cost number 7,2 建立乙個集合 create or replace type projectlist is tab...

PLSQL集合型別的使用總結

plsql集合型別的使用總結 在pl sql 中,集合 collection 是一組有序的元素組成的物件,這些元素的型別必須一致。pl sql 將collection 分成3 類,分別為associative arrays 也稱index by tables nested tables varray...

PLSQL集合總結

一 索引表 描述1 索引表只能作為pl sql符合資料型別使用,而不能作為表列的資料型別使用。2 索引表的下標可以為負值,索引表的下標沒有限制。語法type index table type is table of element type index by varchar2 binary inte...