2

我在包的规范中定义了以下类型

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,奥雷尔

4

3 回答 3

3

您应该在访问它们之前正确初始化所有集合(包括嵌套)。
它们在初始化之前是原子的空值。

t_info := table_info('gl_temp_report1', c_info());

您还必须extend在为每个 varray 元素分配值之前调用(或使用 扩展一次extend(3))。
或者在一个语句中完成所有操作:

t_info := table_info('gl_temp_report1', c_info('table_idx','table_row','table_row_detail'));
于 2013-03-06T15:26:24.740 回答
3

要执行初始化,您需要以类似于以下方式将初始化块添加到包主体:

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;

分享和享受。

于 2013-03-06T16:36:04.190 回答
0

您可以创建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;
/

干杯!

于 2014-04-26T04:04:29.920 回答