PLSQL集合-创新互联

PLSQL集合

  • 索引表(或者叫做关联数组,associative array )

    在成都等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站设计、成都网站建设 网站设计制作定制网站建设,公司网站建设,企业网站建设,品牌网站建设,全网整合营销推广,成都外贸网站建设公司,成都网站建设费用合理。
  • 嵌套表(nested table)

  • 变长数组(varray)

  • 二维数组(多层集合)

索引表

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

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; /

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

新闻名称:PLSQL集合-创新互联
转载来源:https://www.cdcxhl.com/article8/dhciip.html

成都网站建设公司_创新互联,为您提供商城网站面包屑导航微信小程序云服务器小程序开发域名注册

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都做网站