1、语法
Type Record_Table_Type_Name Is Table Of Record_Type
Index By pls_integer | binary_integer | varchar2 ;
用于处理多行多列数据
实例:
Declare
Type Record_Table_Type Is Table Of scott.dept%Rowtype Index By Pls_Integer;
Record_Table_Obj Record_Table_Type;
strCur Varchar2(2);
Begin
Select * Into Record_Table_Obj(-1) From scott.dept Where deptno=10;
Select * Into Record_Table_Obj(0) From scott.dept Where deptno=20;
Select * Into Record_Table_Obj(1) From scott.dept Where deptno=30;
If Record_Table_Obj.Count>0 Then
dbms_output.put_line('--从第一个元素开始输出');
strCur:=Record_Table_Obj.First();
Loop
Null;
dbms_output.put_line('部门号:'||Record_Table_Obj(strCur).deptno || ' 部门名:'||Record_Table_Obj(strCur).dname);
Exit When strCur=Record_Table_Obj.Last;
strCur:=Record_Table_Obj.Next(strCur);
End Loop;
dbms_output.put_line('--从最后一个元素开始输出');
strCur:=Record_Table_Obj.Last();
Loop
Null;
dbms_output.put_line('部门号:'||Record_Table_Obj(strCur).deptno || ' 部门名:'||Record_Table_Obj(strCur).dname);
Exit When strCur=Record_Table_Obj.First;
strCur:=Record_Table_Obj.Prior(strCur);
End Loop;
End If;
End;
输出:
--从第一个元素开始输出
部门号:10 部门名:ACCOUNTING
部门号:20 部门名:RESEARCH
部门号:30 部门名:SALES
--从最后一个元素开始输出
部门号:30 部门名:SALES
部门号:20 部门名:RESEARCH
部门号:10 部门名:ACCOUNTING
Declare
Type Record_Table_Type Is Table Of scott.dept%Rowtype Index By Varchar2(2);
Record_Table_Obj Record_Table_Type;
strCur Varchar2(2);
Begin
Select * Into Record_Table_Obj('a') From scott.dept Where deptno=10;
Select * Into Record_Table_Obj('b') From scott.dept Where deptno=20;
Select * Into Record_Table_Obj('c') From scott.dept Where deptno=30;
If Record_Table_Obj.Count>0 Then
dbms_output.put_line('--从第一个元素开始输出');
strCur:=Record_Table_Obj.First();
Loop
Null;
dbms_output.put_line('部门号:'||Record_Table_Obj(strCur).deptno || ' 部门名:'||Record_Table_Obj(strCur).dname);
Exit When strCur=Record_Table_Obj.Last;
strCur:=Record_Table_Obj.Next(strCur);
End Loop;
dbms_output.put_line('--从最后一个元素开始输出');
strCur:=Record_Table_Obj.Last();
Loop
Null;
dbms_output.put_line('部门号:'||Record_Table_Obj(strCur).deptno || ' 部门名:'||Record_Table_Obj(strCur).dname);
Exit When strCur=Record_Table_Obj.First;
strCur:=Record_Table_Obj.Prior(strCur);
End Loop;
End If;
End;
输出:
--从第一个元素开始输出
部门号:10 部门名:ACCOUNTING
部门号:20 部门名:RESEARCH
部门号:30 部门名:SALES
--从最后一个元素开始输出
部门号:30 部门名:SALES
部门号:20 部门名:RESEARCH
部门号:10 部门名:ACCOUNTING
2、Limit 返回集合对象的最大元素个数 如果不限制,则返回Null
Declare
Type Record_Table_Type Is Table Of scott.dept%Rowtype Index By Pls_Integer;
Record_Table_Obj Record_Table_Type;
nested_table_obj nested_table_Typename:=nested_table_Typename();
varray_obj varray_Typename:=varray_Typename();
strCur Varchar2(2);
Begin
dbms_output.put_line('当前元素个数:'||Record_Table_Obj.Count);
dbms_output.put_line('当前元素个数:'||nested_table_obj.Count);
dbms_output.put_line('当前元素个数:'||varray_obj.Count);
dbms_output.put_line('最大元素个数:'||Record_Table_Obj.Limit);
dbms_output.put_line('最大元素个数:'||nested_table_obj.limit);
dbms_output.put_line('最大元素个数:'||varray_obj.limit);
End;
当前元素个数:0
当前元素个数:0
当前元素个数:0
最大元素个数:
最大元素个数:
最大元素个数:100
3、以下方法适用于嵌套表和变长数组
Extend() :为集合对象添加一个Null元素
Extend(n):为结合对象添加n个Null元素
Extend(n,i):为集合对象添加n个元素 ,元素值和第i个元素相同
Trim():从集合对象尾部删除一个元素
Trim():从集合对象尾部删除n个元素
Declare
nested_table_obj nested_table_Typename:=nested_table_Typename();
strCur Varchar2(2);
Begin
dbms_output.put_line('当前元素个数:'||nested_table_obj.Count);
nested_table_obj.extend();
dbms_output.put_line('');
dbms_output.put_line('当前元素个数:'||nested_table_obj.Count);
nested_table_obj.extend(2);
dbms_output.put_line('');
dbms_output.put_line('当前元素个数:'||nested_table_obj.Count);
dbms_output.put_line('');
strCur:=nested_table_obj.First;
Loop
dbms_output.put_line('第'||strCur||'个元素值:'||nested_table_obj(strCur));
Exit When strCur=nested_table_obj.Last;
strCur:=nested_table_obj.Next(strCur);
End Loop;
nested_table_obj(1):='a';
nested_table_obj.Extend(5,1);
strCur:=nested_table_obj.First;
dbms_output.put_line('');
Loop
dbms_output.put_line('第'||strCur||'个元素值:'||nested_table_obj(strCur));
Exit When strCur=nested_table_obj.Last;
strCur:=nested_table_obj.Next(strCur);
End Loop;
nested_table_obj.Trim;
strCur:=nested_table_obj.First;
dbms_output.put_line('');
Loop
dbms_output.put_line('第'||strCur||'个元素值:'||nested_table_obj(strCur));
Exit When strCur=nested_table_obj.Last;
strCur:=nested_table_obj.Next(strCur);
End Loop;
nested_table_obj.Trim(3);
strCur:=nested_table_obj.First;
dbms_output.put_line('');
Loop
dbms_output.put_line('第'||strCur||'个元素值:'||nested_table_obj(strCur));
Exit When strCur=nested_table_obj.Last;
strCur:=nested_table_obj.Next(strCur);
End Loop;
End;
输出:
当前元素个数:0
当前元素个数:1
当前元素个数:3
第1个元素值:
第2个元素值:
第3个元素值:
第1个元素值:a
第2个元素值:
第3个元素值:
第4个元素值:a
第5个元素值:a
第6个元素值:a
第7个元素值:a
第8个元素值:a
第1个元素值:a
第2个元素值:
第3个元素值:
第4个元素值:a
第5个元素值:a
第6个元素值:a
第7个元素值:a
第1个元素值:a
第2个元素值:
第3个元素值:
第4个元素值:a
4、以下方法适用于嵌套表和索引表
Delete():删除集合对象的所有元素
Delete(n):删除集合对象的第n个元素
Delete(m,n):删除集合对象的第m到n个元素
Declare
nested_table_obj nested_table_Typename:=nested_table_Typename('a','b','c');
strCur Varchar2(2);
Begin
strCur:=nested_table_obj.First;
Loop
dbms_output.put_line('第'||strCur||'个元素值:'||nested_table_obj(strCur));
Exit When strCur=nested_table_obj.Last;
strCur:=nested_table_obj.Next(strCur);
End Loop;
dbms_output.put_line('');
nested_table_obj.delete;
dbms_output.put_line(nested_table_obj.count);
dbms_output.put_line('');
nested_table_obj:=nested_table_typename('a','b','c');
nested_table_obj.delete(2);
strCur:=nested_table_obj.First;
Loop
dbms_output.put_line('第'||strCur||'个元素值:'||nested_table_obj(strCur));
Exit When strCur=nested_table_obj.Last;
strCur:=nested_table_obj.Next(strCur);
End Loop;
dbms_output.put_line('');
nested_table_obj:=nested_table_typename('a','b','c','d','e');
nested_table_obj.delete(2,4);
strCur:=nested_table_obj.First;
Loop
dbms_output.put_line('第'||strCur||'个元素值:'||nested_table_obj(strCur));
Exit When strCur=nested_table_obj.Last;
strCur:=nested_table_obj.Next(strCur);
End Loop;
End;
输出:
第1个元素值:a
第2个元素值:b
第3个元素值:c
0
第1个元素值:a
第3个元素值:c
第1个元素值:a
第5个元素值:e