PLSQL集合

发布时间:2020-07-31 02:05:22 作者:断情漠
来源:网络 阅读:693

PLSQL集合

索引表

---创建索引表类型的语法如下所示:               

TYPE  type_name IS TABLE OF  element_type

   INDEX BY index_type;  

table_name  TYPE_NAME;

--其中,element_type 指明集合中存放的数据的类型

--index_type指定下标的类型。只能是整型或者字符串

--使用下标来引用索引表中的单个元素,如下所示:

table_name(index);

---示例1:分别声明一个游标和一个索引表类型,游标

--student表中检索出前10个学生的姓名。遍历游标,

--将每个学生的姓名保存到一个索引表类型的集合中,

--然后从集合中取出每个学生的姓名打印到屏幕上

declare
  --声明游标,保存10个学生姓名
 cursor c_student is
   select last_name
     from student
     where rownum <= 10; 
  --声明索引表集合类型
 type last_name_type is table of student.last_name%type
   index by pls_integer;   
  --声明集合变量
 last_name_tab last_name_type;
  --声明下标变量
 v_index pls_integer := 0;
begin
  --遍历游标
  forr_student in c_student loop
   v_index := v_index + 1;
   --将学生姓名保存到集合中
   last_name_tab(v_index) := r_student.last_name; 
  endloop;
  --遍历集合
  fori in 1..10 loop
   dbms_output.put_line( last_name_tab(i));
  endloop;
  --注意:引用不存在的集合元素会抛出NO_DATA_FOUND异常。
  --例如
 --dbms_output.put_line(last_name_tab(11));
end;

嵌套表

创建嵌套表的语法如下所示:

TYPE type_name IS TABLEOF element_type;

table_name  TYPE_NAME;

--该声明非常类似于索引表的声明,只是没有INDEXBY子句。

--嵌套表的下标类型固定为Integer整型

declare
  --声明游标,保存10个学生姓名
 cursor c_student is
   select last_name
     from student
     where rownum <= 10;   
  --声明嵌套表集合类型
 type last_name_type is table of student.last_name%type;
  --声明集合变量。必须使用构造器函数进行初始化
  last_name_tab last_name_type := last_name_type();
  --声明下标变量
  v_indexpls_integer := 0;
begin
  --遍历游标
  forr_student in c_student loop
   v_index := v_index + 1;
   --必须调用extend方法添加存储空间
    last_name_tab.extend;         --和数组一样,每赋值一个元素需调用extend
   --将学生姓名保存到集合中
   last_name_tab(v_index) := r_student.last_name; 
  endloop;
  --遍历集合
  fori in 1..10 loop
   dbms_output.put_line( last_name_tab(i));
  endloop; 
end;

变长数组

创建变长数组的语法如下所示:

TYPE  type_name IS VARRAY(size_limit) OFelement_type ;

varray_name  TYPE_NAME;

--size_limit:最大元素个数

 

--它和嵌套表类型的区别是:他有最大元素个数限制

--修改上例,使用保长数组类型

declare
  --声明游标,保存10个学生姓名
 cursor c_student is
   select last_name
     from student
     where rownum <= 10;
  --声明变长数组集合类型
  type last_name_type is varray(10) of student.last_name%type; 
  --声明集合变量。必须使用构造器函数进行初始化
  last_name_tab last_name_type := last_name_type();
  --声明下标变量
 v_index pls_integer := 0;
begin
  --遍历游标
  forr_student in c_student loop
   v_index := v_index + 1;
   --必须调用extend方法添加存储空间
    last_name_tab.extend;   --每赋值一个元素需调用extend
   --将学生姓名保存到集合中
   last_name_tab(v_index) := r_student.last_name; 
  endloop;
  --遍历集合
  fori in 1..10 loop
   dbms_output.put_line( last_name_tab(i));
  endloop; 
end;

--可见,变长数组在编码使用的限制和嵌套表完全相同。

二维数组

Oracle 9i开始,PL/SQL允许创建元素类型为集合类型的集合。这种集合被称为多层集合。

二维数组:有一个一维数组,其中的每个元素又是一个一维数组,那么这个一维数组叫做二维数组。

为引用这个多层集合中单独的元素,需要使用如下语法:

varray_name(subscript  of the outer  varray)

          (subscript  of the  inner varray)

declare
  --声明变长数组类型
  typevarray_type1 is varray(4) of number;
  --声明多层集合(二维数组)
  typevarray_type2 is varray(3) of varray_type1;
  varray1varray_type1 := varray_type1(2,4,6,8);
  varray2varray_type2 := varray_type2(varray1);
begin
 varray2.extend;  --调用extend
  varray2(2):= varray_type1(1,3,5,7);
 varray2.extend;  --调用extend
  varray2(3):= varray_type1(8,8,8,8); 
  --遍历集合
  for i in1..3 loop
    for j in1..4 loop
      dbms_output.put_line('varray2('||i||')('||j
      ||')='||varray2(i)(j));
    end loop;
  end loop;
 dbms_output.put_line('-------------------------------');
 
  --遍历集合,实际的写法
  for i invarray2.first..varray2.last loop
    for j invarray2(i).first..varray2(i).last loop
     dbms_output.put_line('varray2('||i||')('||j
      ||')='||varray2(i)(j));
    end loop;
  end loop;
end;
/


推荐阅读:
  1. oracle plsql utl_file
  2. PLSQL 之类型、变量和结构

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

二维数组 索引表 嵌套表

上一篇:asp.net开发日常注意事项

下一篇:暑假菜鸟学记---html/css

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》