oracle Plsql 未初始化的集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15251201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plsql Uninitialized collections
提问by user1540471
I have the following types defined into a spec of a package
我在包的规范中定义了以下类型
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);
In declaration part package body I have
在声明部分包体中我有
t_info table_info;
Inside of a procedure in the body of package I have
在包主体中的程序内部我有
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';
Even package compile succesfully , at runtime I get the exception ORA-06531: Reference to uninitialized collection .
How I initialize col_info collection ?
I tried to initialize t_info.col_info() but I get "There is no function" like this one . TIA, Aurel
即使包编译成功,在运行时我也得到异常 ORA-06531: Reference to uninitialized collection 。
我如何初始化 col_info 集合?我试图初始化 t_info.col_info() 但我得到“没有功能”这样的。TIA, 奥雷尔
回答by Egor Skriptunoff
You should initialize all collections (including nested) properly before accessing them.
They are atomically nulls before initialization.
在访问它们之前,您应该正确初始化所有集合(包括嵌套)。
它们在初始化之前是原子空值。
t_info := table_info('gl_temp_report1', c_info());
You also must call extend
before assigning a value for each varray element (or extend once with extend(3)
).
Or do it all in one statement:
您还必须extend
在为每个 varray 元素分配值之前调用(或使用 扩展一次extend(3)
)。
或者在一个语句中完成所有操作:
t_info := table_info('gl_temp_report1', c_info('table_idx','table_row','table_row_detail'));
回答by Bob Jarvis - Reinstate Monica
To perform initialization you'll need to add an initialization block to the package body, in a manner similar to the following:
要执行初始化,您需要以类似于以下的方式向包主体添加一个初始化块:
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;
Share and enjoy.
分享和享受。
回答by Gurwinder Singh
You can create t_infoas shown:
您可以创建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;
/
Cheers!
干杯!