ORA-06531:Reference to uninitialized collection 问题解决
ORA-06531:Reference to uninitialized collection 问题解决
错误信息:
ORA-06531:Reference to uninitialized collection
错误SQL代码:
declare TYPE t_student_var IS TABLE OF VARCHAR2(100); v_tbl_name t_student_var; begin select name into v_tbl_name(1) from t_student where gid = 1; select name into v_tbl_name(2) from t_student where gid = 2; select name into v_tbl_name(3) from t_student where gid = 3; dbms_output.put_line(v_tbl_name(1)); dbms_output.put_line(v_tbl_name(2)); dbms_output.put_line(v_tbl_name(3)); end;
问题分析:
Oracle自定义类型语法:
TYPE type_name IS TABLE OF element_type INDEX BY [BINARY_INTEGER | PLS_INTEGER | VARRAY2]; 其中:INDEX BY: 该语句的作用是使Number类型的下标自增长,,自动初始化,并分配空间,有了该语句,向表记录插入元素时,不需要显示初始化,也不需要通过extend分配空间。Binary_Integer 与 Pls_Integer 都是整型类型.
Binary_Integer类型变量值计算是由Oracle来执行,不会出现溢出,但是执行速度较慢,因为它是由Oracle模拟执行。而Pls_Integer的执行是由硬件即直接由CPU来运算,因而会出现溢出,但其执行速度较前者快许多。
通过上面的说明可以知道解决的办法有两个:
1、定义记录表类型时,要加上INDEX BY语句,修改之后如下:
declare TYPE t_student_var IS TABLE OF VARCHAR2(100) INDEX BY BINARY_INTEGER; v_tbl_name t_student_var; begin select name into v_tbl_name(1) from t_student where gid = 1; select name into v_tbl_name(2) from t_student where gid = 2; select name into v_tbl_name(3) from t_student where gid = 3; dbms_output.put_line(v_tbl_name(1)); dbms_output.put_line(v_tbl_name(2)); dbms_output.put_line(v_tbl_name(3)); end;
这种方式比较简易,建议使用这种方式.
2、初始化,并使用extend语句扩展空间,修改之后如下:
declare TYPE t_student_var IS TABLE OF VARCHAR2(100); v_tbl_name t_student_var := t_student_var();--初始化 begin v_tbl_name.extend;--扩展空间 select name into v_tbl_name(1) from t_student where gid = 1; v_tbl_name.extend;--扩展空间 select name into v_tbl_name(2) from t_student where gid = 2; v_tbl_name.extend;--扩展空间 select name into v_tbl_name(3) from t_student where gid = 3; dbms_output.put_line(v_tbl_name(1)); dbms_output.put_line(v_tbl_name(2)); dbms_output.put_line(v_tbl_name(3)); end;
另外,使用extend(n),一次性扩展n个空间,所以下面代码和上面的效果是一样的:
declare TYPE t_student_var IS TABLE OF VARCHAR2(100); v_tbl_name t_student_var := t_student_var();--初始化 begin v_tbl_name.extend(3);--扩展3个空间 select name into v_tbl_name(1) from t_student where gid = 1; select name into v_tbl_name(2) from t_student where gid = 2; select name into v_tbl_name(3) from t_student where gid = 3; dbms_output.put_line(v_tbl_name(1)); dbms_output.put_line(v_tbl_name(2)); dbms_output.put_line(v_tbl_name(3)); end
以上就是ORA-06531:Reference to uninitialized collection 问题解决的内容,更多相关内容请关注PHP中文网(www.php.cn)!
上一篇: 关于php开发项目设计文档的有关问题