在PL / SQL中使用记录表

时间:2009-10-22 12:18:27

标签: oracle plsql

我在PL / SQL包中声明了以下类型:

TYPE t_simple_object IS RECORD (
   wert   NUMBER,
   gs     NUMBER,
   vl     NUMBER);

TYPE t_obj_table IS TABLE OF t_simple_object
  INDEX BY BINARY_INTEGER;

然后我宣布一个变量:

obj t_obj_table;

但是,当我想使用变量时,我无法初始化或扩展它:

obj := t_obj_table ();

给出以下错误:

PLS-00222: no function with name 'T_OBJ_TABLE' exists in this scope

如果我没有初始化它,我无法扩展它以添加一些日期

obj.EXTEND();

给出了另一个错误:

PLS-00306: wrong number or types of arguments in call to 'EXTEND'

我该如何做到这一点?

4 个答案:

答案 0 :(得分:22)

你没有扩展一个由“某事”索引的表,你可以使用它......

DECLARE
   TYPE t_simple_object IS RECORD 
      ( wert   NUMBER
      , gs     NUMBER
      , vl     NUMBER
      ); 

   TYPE t_obj_table IS TABLE OF t_simple_object
   INDEX BY BINARY_INTEGER; 

   my_rec t_simple_object;
   obj t_obj_table; 
BEGIN
   my_rec.wert := 1;
   my_rec.gs := 1;
   my_rec.vl := 1;
   obj(1) := my_rec;
END;
/

要使用EXTEND语法,此示例应该这样做......

DECLARE
   TYPE t_simple_object IS RECORD 
      ( wert   NUMBER
      , gs     NUMBER
      , vl     NUMBER
      ); 

   TYPE t_obj_table IS TABLE OF t_simple_object; 

   my_rec t_simple_object;
   obj t_obj_table := t_obj_table(); 
BEGIN
   obj.EXTEND;
   my_rec.wert := 1;
   my_rec.gs := 1;
   my_rec.vl := 1;
   obj(1) := my_rec;
END;
/

另见this link (Ask Tom)

答案 1 :(得分:8)

您无法扩展关联数组。 只需为其指定值

declare
  TYPE t_simple_object IS RECORD (
    wert   NUMBER,
    gs     NUMBER,
    vl     NUMBER);

  TYPE t_obj_table IS TABLE OF t_simple_object INDEX BY BINARY_INTEGER;

  simple_object t_simple_object;
begin
  simple_object.wert := 1;
  simple_object.gs := 2;
  simple_object.vl := 3;
  obj(1) := simple_object;
end;
/

答案 2 :(得分:8)

如果您不想使用关联数组(也就是索引表),则省略“INDEX BY BINARY_INTEGER”子句。然后您的代码可以正常工作:

declare 
    TYPE t_simple_object IS RECORD (
       wert   NUMBER,
       gs     NUMBER,
       vl     NUMBER);
    TYPE t_obj_table IS TABLE OF t_simple_object;
    obj t_obj_table;
begin  
    obj := t_obj_table ();
    obj.EXTEND();
end;

答案 3 :(得分:0)

或者只使用记录(或关联的记录数组)

create or replace package p_test is

  type t_rec is record (
    empname varchar2(50),
    empaddr varchar2(50));

  function p_test_ret_record return t_rec;

end p_test;


create or replace package body p_test is

  function p_test_ret_record return t_rec is
     l_rec t_rec;
  begin
     l_rec.empname := 'P1';
     l_rec.empaddr := 'P2';     
     return l_rec;
  end;

end p_test;

declare
  -- Non-scalar parameters require additional processing 
  result p_test.t_rec;
begin
  -- Call the function
  result := p_test.p_test_ret_record;
  dbms_output.put_line('Name: ' || result.empname || ' Addr: ' || result.empaddr);
end;