VARRAY是在Oracle8G的时候引入的一个类型,是一种密集填充数组,更容易被理解成为一个传统的数组。在创建这些数组的时候,都有一个尺寸,需要非常注意的是VARRAY的下表是从1开始的,在其他很多变成语言中,数组的下表都是从0开始的,比如JAVA。在实际的使用中,初学者经常会出现如下的错误提示:
cuug每周五晚8点都有免费网络课程,详情可点击cuug官网。
ORA-06533:下表超出数量
例如下面的代码就会出现该错误:
DECLARE
TYPE NUM_VARRAY IS VARRAY(100) OF NUMBER NOT NULL;
V_NUM_VARRAY NUM_VARRAY:=NUM_VARRAY();
BEGIN
FOR J IN 1 .. 100 LOOP
V_NUM_VARRAY(J) := J;
END LOOP;
FOR I IN 1 .. V_NUM_VARRAY.COUNT LOOP
DBMS_OUTPUT.PUT_LINE(TO_CHAR(V_NUM_VARRAY(I)));
END LOOP;
END;
这个错误的原因是什么呢?我们定义了一个为空的VARRAY,在第一个J循环的时候,将1赋值给V_NUM_ARRAY(1),但是因为实际上,该集合变量并没有存储空间,因此PL/SQL就会报一个“下表超出数量”的错误。
如何去解决这个问题呢?查阅Oracle 的MOS 找到了如下描述:
EXTEND Collection Method
EXTEND is a procedure that adds elements to the end of a varray or nested table. The collection can be empty, but not null. (To make a collection empty or add elements to a null collection, use a constructor. For more information, see "Collection Constructors".)
The EXTEND method has these forms:
EXTEND appends one null element to the collection.
EXTEND(n) appends n null elements to the collection.
EXTEND(n,i) appends n copies of the ith element to the collection.
Note:
EXTEND(n,i) is the only form that you can use for a collection whose elements have the NOT NULL constraint.
EXTEND operates on the internal size of a collection. That is, if DELETE deletes an element but keeps a placeholder for it, then EXTEND considers the element to exist.
Example 5-20 declares a nested table variable, initializing it with three elements; appends two copies of the first element; deletes the fifth (last) element; and then appends one null element. Because EXTEND considers the deleted fifth element to exist, the appended null element is the sixth element. The procedureprint_nt prints the nested table variable after initialization and after the EXTEND and DELETE operations. The type nt_type and procedure print_nt are defined inExample 5-6.
Example 5-20 EXTEND Method with Nested Table
DECLARE
nt nt_type := nt_type(11, 22, 33);
BEGIN
print_nt(nt);
nt.EXTEND(2,1); -- Append two copies of first element
print_nt(nt);
nt.DELETE(5); -- Delete fifth element
print_nt(nt);
nt.EXTEND; -- Append one null element
print_nt(nt);
END;
/
Result:
nt.(1) = 11
nt.(2) = 22
nt.(3) = 33
---
nt.(1) = 11
nt.(2) = 22
nt.(3) = 33
nt.(4) = 11
nt.(5) = 11
---
nt.(1) = 11
nt.(2) = 22
nt.(3) = 33
nt.(4) = 11
---
nt.(1) = 11
nt.(2) = 22
nt.(3) = 33
nt.(4) = 11
nt.(6) = NULL
---
上面的大意是:
EXTEND方法为VARRAY或者NESTED TABLE集合中的一个或者多个新元素分配存储空间。有两个可以选择的选项,默认情况下,是它在向集合添加值之前的时候,为该值分配存储空间。他的两个参数的意思分别是,第一个参数是增加多少个元素的空间,第二个参数的意思是引用集合中已经存在的元素的索引,该元素就会被复制到新的元素上。
上面出错的代码修改为如下可以正常运行了:
DECLARE
TYPE NUM_VARRAY IS VARRAY(100) OF NUMBER NOT NULL;
V_NUM_VARRAY NUM_VARRAY:=NUM_VARRAY();
BEGIN
FOR J IN 1 .. 100 LOOP
V_NUM_VARRAY.EXTEND;
V_NUM_VARRAY(J) := J;
END LOOP;
FOR I IN 1 .. V_NUM_VARRAY.COUNT LOOP
DBMS_OUTPUT.PUT_LINE(TO_CHAR(V_NUM_VARRAY(I)));
END LOOP;
END;
分享到:
相关推荐
ORA-12154: TNS: 无法解析指定的连接标识符的解决方法
ora-01033:oracle initialization or shutdown in progress 解决方法 ora-01033:oracle initialization or shutdown in progress 解决方法 ora-01033:oracle initialization or shutdown in progress 解决方法 ora-...
ORA-32001:write to spfile requested but no spfile is in use请求写入spfile,但没有使用spfile的解决方法 在输入以下语句中报了这样的错误: SQL>alter system set control_files=’/u01/app/oracle/oradata/prod/...
在运行查询SELECT * FROM V$SESSION 会出现ORA-29275:部分多字节字符的错误,这是什么原因开始我不得其解,网上也没有介绍什么好办法。本文给出答案。
ORA-01036:非法的变量名/编号 oracle特有的错误
错误描述:oracle远程连接服务器出现 ORA-12170 TNS:连接超时 错误检查:有很多是oracle自身安装的问题,但是我这里服务器配置正常,监听正常,服务正常,远程可以ping通服务器。 这里主要是防火墙问题,解决办法: ...
ORA-00031:标记要终止的会话解决方案; ORA-00031:标记要终止的会话解决方案;
在oracle里面运行一下,解决Exception java.sql.SQLException ORA-00600 内部错误代码
前几天升级Oracle数据库,从11.2.0.1升级至11.2.0.4,安装完成后,打开PL/SQL,显示ORA-12514,如图: 问题阐述 ORA-12514:监听程序当前无法识别链接描述符中请求的服务,简单的来说就是Oracle数据库的监听器配置有...
有关表死锁的详细图片 博文链接:https://meteor-1988.iteye.com/blog/1568695
NULL 博文链接:https://3w1h.iteye.com/blog/1211779
调整参数后引起,ORA-00838: Specified value of MEMORY_TARGET is too small, needs to be at least 3 2768M 的解决方案
最近在工作中遇到了一个问题,错误是Oracle dbca时报错:ORA-12547: TNS:lost contact,通过查找相关的资料终于找到了解决的方法,下面分享给大家,话不多说了,来一起看看详细的介绍吧。 环境: OS:RHEL6.5 x86-...
ORA-12541 TNSno listener 的解决方案 ORA-12541 TNSno listener 的解决方案
oracle 12 c ORA-01017: 用户名/口令无效; 登录被拒绝
-- 解决ORA-00904: "WMSYS"."WM_CONCAT": 标识符无效 的文件包....... -- 解决ORA-00904: "WMSYS"."WM_CONCAT": 标识符无效 的文件包....... -- 解决ORA-00904: "WMSYS"."WM_CONCAT": 标识符无效 的文件包....... ...
用oracle数据库新建连接时遇到ora-12505,此问题解决后又出现ora-12519错误,郁闷的半天,经过一番折腾问题解决,下面小编把我的两种解决方案分享给大家,仅供参考。 解决方案一: 今天工作时在新建连接的时候遇到...
oracle网络配置(listener_ora-sqlnet_ora-tnsnames_ora).mht
sqlplus / as sysdba时出现ORA-01031 insufficient privilege