Oracle 集合 plsql记录表

本文详细介绍了SQL中集合对象的使用方法,包括类型定义、实例创建、元素操作、限制属性等核心内容,通过实例展示了如何高效地处理多行多列数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值