Plsql 未初始化的集合
Posted
技术标签:
【中文标题】Plsql 未初始化的集合【英文标题】:Plsql Uninitialized collections 【发布时间】:2013-03-06 15:22:04 【问题描述】:我在包的规范中定义了以下类型
type column_info is record (col_name varchar2(20), col_value varchar2(1000));
type c_info is varray(10) of column_info;
type table_info is record (table_name varchar2(20), col_info c_info);
在声明部分包体中我有
t_info table_info;
在我拥有的包体中的过程内部
t_info:=null;
t_info.table_name:='gl_temp_report1';
t_info.col_info(1).col_name:='table_idx';
t_info.col_info.extend;
t_info.col_info(2).col_name:='table_row';
t_info.col_info.extend;
t_info.col_info(3).col_name:='table_row_detail';
即使包编译成功,在运行时我也会收到异常 ORA-06531: Reference to uninitialized collection。 我如何初始化 col_info 集合? 我试图初始化 t_info.col_info() 但我得到像这样的“没有功能”。 TIA,奥雷尔
【问题讨论】:
ora-06531.ora-code.com 并阅读底部的帖子,这可能会对您有所帮助。 【参考方案1】:您应该在访问它们之前正确初始化所有集合(包括嵌套)。 它们在初始化之前是原子的空值。
t_info := table_info('gl_temp_report1', c_info());
您还必须在为每个可变数组元素分配值之前调用extend
(或使用extend(3)
扩展一次)。
或者在一个语句中完成所有操作:
t_info := table_info('gl_temp_report1', c_info('table_idx','table_row','table_row_detail'));
【讨论】:
我得到“此范围内不存在名称为 'TABLE_INFO' 的函数 @user1540471 - 对不起,我忘记了记录,不像对象,没有方便的构造函数。每个记录字段都应单独初始化。t_info.table_name:='gl_temp_report1';
和 t_info.col_info:=c_info();
【参考方案2】:
要执行初始化,您需要将初始化块添加到包主体,方式类似于以下:
CREATE OR REPLACE PACKAGE BODY your_package IS
t_info table_info;
-- Whatever other procedure definitions, etc, are needed
BEGIN -- package initialization
t_info.table_name:='gl_temp_report1';
t_info.col_info := c_info();
t_info.col_info.extend;
t_info.col_info(1).col_name:='table_idx';
t_info.col_info.extend;
t_info.col_info(2).col_name:='table_row';
t_info.col_info.extend;
t_info.col_info(3).col_name:='table_row_detail';
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Exception!'); -- Add whatever error handling is needed
END your_package;
分享和享受。
【讨论】:
【参考方案3】:你可以创建t_info如图:
declare
type column_info is record (col_name varchar2(20), col_value varchar2(1000));
type c_info is varray(10) of column_info;
type table_info is record (table_name varchar2(20), col_info c_info);
t_info table_info;
begin
t_info.table_name := null;
t_info.col_info := c_info();
for i in 1..10 loop
t_info.col_info.extend;
t_info.col_info(i).col_name := null;
t_info.col_info(i).col_value := null;
end loop;
end;
/
干杯!
【讨论】:
这不起作用,存在语法错误,并且这些构造函数不适用于 RECORD。以上是关于Plsql 未初始化的集合的主要内容,如果未能解决你的问题,请参考以下文章