Oracle 11g Release 1 (11.1) PL/SQL_多维 Collection 类型和其异常
本文内容
- 多维 Collection
- Collection 异常
多维 Collection
虽然 collection 只有一维的,但可以模型一个多维的。创建一个 collection,其每个元素也是 collection 。例如,创建一个 varray 的 nested table,一个 varray 的 varray,一个 nested table 的 varray 等。
示例1:演示多维 varray
DECLARE
TYPE t1 IS VARRAY(10) OF INTEGER;
TYPE nt1 IS VARRAY(10) OF t1; -- multilevel varray type
va t1 := t1(2,3,5);
-- initialize multilevel varray
nva nt1 := nt1(va, t1(55,6,73), t1(2,4), va);
i INTEGER;
va1 t1;
BEGIN
-- multilevel access
i := nva(2)(3); -- i will get value 73
DBMS_OUTPUT.PUT_LINE('I = ' || i);
-- add a new varray element to nva
nva.EXTEND;
-- replace inner varray elements
nva(5) := t1(56, 32);
nva(4) := t1(45,43,67,43345);
-- replace an inner integer element
nva(4)(4) := 1; -- replaces 43345 with 1
-- add a new element to the 4th varray element
-- and store integer 89 into it.
nva(4).EXTEND;
nva(4)(5) := 89;
END;
/
示例2:演示多维 nested table
DECLARE
TYPE tb1 IS TABLE OF VARCHAR2(20);
TYPE Ntb1 IS TABLE OF tb1; -- table of table elements
TYPE Tv1 IS VARRAY(10) OF INTEGER;
TYPE ntb2 IS TABLE OF tv1; -- table of varray elements
vtb1 tb1 := tb1('one', 'three');
vntb1 ntb1 := ntb1(vtb1);
vntb2 ntb2 := ntb2(tv1(3,5), tv1(5,7,3));
-- table of varray elements
BEGIN
vntb1.EXTEND;
vntb1(2) := vntb1(1);
-- delete the first element in vntb1
vntb1.DELETE(1);
-- delete the first string
-- from the second table in the nested table
vntb1(2).DELETE(1);
END;
/
示例3:演示多维 associative array
DECLARE
TYPE tb1 IS TABLE OF INTEGER INDEX BY PLS_INTEGER;
-- the following is index-by table of index-by tables
TYPE ntb1 IS TABLE OF tb1 INDEX BY PLS_INTEGER;
TYPE va1 IS VARRAY(10) OF VARCHAR2(20);
-- the following is index-by table of varray elements
TYPE ntb2 IS TABLE OF va1 INDEX BY PLS_INTEGER;
v1 va1 := va1('hello', 'world');
v2 ntb1;
v3 ntb2;
v4 tb1;
v5 tb1; -- empty table
BEGIN
v4(1) := 34;
v4(2) := 46456;
v4(456) := 343;
v2(23) := v4;
v3(34) := va1(33, 456, 656, 343);
-- assign an empty table to v2(35) and try again
v2(35) := v5;
v2(35)(2) := 78; -- it works now
END;
/
Collection 异常
示例 4:演示 Collection 异常
演示各种 PL/SQL 预定义的 collection 异常,备注的部分说明如何避免这些异常。
DECLARE
TYPE WordList IS TABLE OF VARCHAR2(5);
words WordList;
err_msg VARCHAR2(100);
PROCEDURE display_error IS
BEGIN
err_msg := SUBSTR(SQLERRM, 1, 100);
DBMS_OUTPUT.PUT_LINE('Error message = ' || err_msg);
END;
BEGIN
BEGIN
words(1) := 10; -- Raises COLLECTION_IS_NULL
-- A constructor has not been used yet.
-- Note: This exception applies to varrays and nested tables,
-- but not to associative arrays which do not need a constructor.
EXCEPTION
WHEN OTHERS THEN display_error;
END;
-- After using a constructor, you can assign values to the elements.
words := WordList('1st', '2nd', '3rd'); -- 3 elements created
-- Any expression that returns a VARCHAR2(5) is valid.
words(3) := words(1) || '+2';
BEGIN
words(3) := 'longer than 5 characters'; -- Raises VALUE_ERROR
-- The assigned value is too long.
EXCEPTION
WHEN OTHERS THEN display_error;
END;
BEGIN
words('B') := 'dunno'; -- Raises VALUE_ERROR
-- The subscript (B) of a nested table must be an integer.
-- Note: Also, NULL is not allowed as a subscript.
EXCEPTION
WHEN OTHERS THEN display_error;
END;
BEGIN
words(0) := 'zero'; -- Raises SUBSCRIPT_OUTSIDE_LIMIT
-- Subscript 0 is outside the allowed subscript range.
EXCEPTION
WHEN OTHERS THEN display_error;
END;
BEGIN
words(4) := 'maybe'; -- Raises SUBSCRIPT_BEYOND_COUNT
-- The subscript (4) exceeds the number of elements in the table.
-- To add new elements, invoke the EXTEND method first.
EXCEPTION
WHEN OTHERS THEN display_error;
END;
BEGIN
words.DELETE(1);
IF words(1) = 'First' THEN NULL; END IF;
-- Raises NO_DATA_FOUND
-- The element with subcript (1) was deleted.
EXCEPTION
WHEN OTHERS THEN display_error;
END;
END;
/
在子块处理产生的异常,执行就会继续。下面是一些异常:
Collection 异常 | 当……产生…… |
COLLECTION_IS_NULL |
尝试操作一个自动为 null 的 collection |
NO_DATA_FOUND |
一个标值指定了一个被删除的元素,或一个 associative array 不存在的元素 |
SUBSCRIPT_BEYOND_COUNT |
一个标值超过了一个 collection 元素的数量 |
SUBSCRIPT_OUTSIDE_LIMIT |
一个标值超出了被允许的范围 |
VALUE_ERROR |
一个标值为 null 或不能转换成键的类型。若键被定义为 PLS_INTEGER,或标值超出范围,则会产生该异常 |
在多数情况下,你可以传递一个不可靠的标值给方法,而不会产生异常。例如,当你传递一个 null 给 DELETE(n) 时,该方法什么都不会做。给已经删除的元素赋值,来替换这个已删除的元素,不会产生 NO_DATA_FOUND 异常。
示例 5:演示 DELETE(n) 如何处理不可靠的标值
DECLARE
TYPE NumList IS TABLE OF NUMBER;
nums NumList := NumList(10,20,30); -- initialize table
BEGIN
nums.DELETE(-1); -- does not raise SUBSCRIPT_OUTSIDE_LIMIT
nums.DELETE(3); -- delete 3rd element
DBMS_OUTPUT.PUT_LINE(nums.COUNT); -- prints 2
nums(3) := 30; -- allowed; does not raise NO_DATA_FOUND
DBMS_OUTPUT.PUT_LINE(nums.COUNT); -- prints 3
END;
/
示例 6:演示包和本地 Collection 之间不兼容性
包的 collection 类型与本地 collection 类型不兼容。例如,若你调用包的存储过程,如下所示,第二个存储过程会调用失败,因为,包的和本地的 varray 类型不兼容,尽管它们具有相同的定义。
CREATE PACKAGE pkg AS
TYPE NumList IS TABLE OF NUMBER;
PROCEDURE print_numlist (nums NumList);
END pkg;
/
CREATE PACKAGE BODY pkg AS
PROCEDURE print_numlist (nums NumList) IS
BEGIN
FOR i IN nums.FIRST..nums.LAST LOOP
DBMS_OUTPUT.PUT_LINE(nums(i));
END LOOP;
END;
END pkg;
/
DECLARE
TYPE NumList IS TABLE OF NUMBER;
n1 pkg.NumList := pkg.NumList(2,4); -- type from the package.
n2 NumList := NumList(6,8); -- local type.
BEGIN
pkg.print_numlist(n1); -- type from pkg is legal
-- The packaged procedure cannot accept
-- a value of the local type (n2)
-- pkg.print_numlist(n2); -- Causes a compilation error.
END;
/
Oracle 11g Release 1 (11.1) PL/SQL_多维 Collection 类型和其异常相关推荐
- Oracle 11g Release 1 (11.1) Data Pump 导出模式
http://docs.oracle.com/cd/B28359_01/server.111/b28319/dp_export.htm#i1007114 本文内容 Data Pump 提供不同的导出模 ...
- Oracle 11g Release 1 (11.1) Data Pump 导入模式
http://docs.oracle.com/cd/B28359_01/server.111/b28319/dp_import.htm#i1007324 本文内容 导入最显着的特点之一就是导入的模式, ...
- Oracle 11g Release 1 (11.1) 游标——显式游标
http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/static.htm#CIHCAHJA 本文内容 声明一个游标 打开一个游标 用游标获取一个 ...
- Oracle 11g Release 2 (11.2) for Microsoft Windows (32-Bit)安装与卸除
卸除 运行如下批处理文件 D:\oraclexe\app\oracle\product\11.2.0\server\deinstall\deinstall.bat 批处理文件在运行结束后自动退出 以下 ...
- Oracle 11g Release 1 (11.1) 单行函数——(返回字符值)字符函数
http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions001.htm#autoId2 本文内容 返回字符值的字符函数 附录 AS ...
- Oracle 11g Release 1 (11.1) 查询优化器的访问路径
http://docs.oracle.com/cd/B28359_01/server.111/b28274/optimops.htm#i82080 本文内容 全表扫描(Full Table Scans ...
- Oracle 11g Release 1 (11.1)——简单管理聚簇
http://docs.oracle.com/cd/B28359_01/server.111/b28310/clustrs.htm#i1006586 本文内容 创建聚簇 变更聚簇 删除聚簇 创建聚簇 ...
- Oracle 11g Release 1 (11.1) Oracle 本文索引的四种类型
http://docs.oracle.com/cd/B28359_01/text.111/b28303/ind.htm#g1020588 本文内容 对于 Oracle 文本,你可以使用 CREATE ...
- [E17264-15]Oracle® Database 2 Day + Real Application Clusters Guide 11g Release 2 (11.2)之六
本文主要摘自于Oracle Database 2 Day + Real Application Clusters Guide 11g Release 2 (11.2) E17264-15,旨在总结和规 ...
最新文章
- 最新OCR开源神器来了!
- 成功解决read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and wil
- 个人计算机既pc机,个人计算机简称pc机这种计算机属于___pc机开机后计算机执行_个人计算机即pc机(14)...
- ASP.NET MVC 入门11、使用AJAX
- 什么是Springmvc以及如果编写第一个入门程序
- 10分钟带你学会微信小程序的反编译
- 中国移动回应“5G消息APP”下架:并非面向客户商用发布的产品
- 图解读写锁:pthead_rwlock_t
- 【Flutter】微信项目实战【06】 通讯录界面搭建(中)
- Linux 安装SNMPWALK工具
- 平衡小车之家--风力摆开箱实录(附资料)
- 如何用数据进行产品运营
- Vmware安装深度Deepin图文
- TCP的核心系列 — ACK的处理(一)
- IAR下载程序只有提示音,没有任何反应。或报错The configuration does not have debuggable output.(A debug-only project shoul
- android 仿网易标签切换,高仿网易云音乐客户端的Home页面切换Tabhost-IT蓝豹
- CentOS 7如何连接无线网络
- 修改PvZ游戏数据文件
- 奇怪问题--二极管并联电阻分压
- 梁宁《产品思维30讲》(附带作业)--怎样理解愉悦、不爽(同理心)