代码之家  ›  专栏  ›  技术社区  ›  Thorsten

使用pl/sql中的记录表

  •  12
  • Thorsten  · 技术社区  · 15 年前

    我已经在我的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 回复  |  直到 8 年前
        1
  •  22
  •   PaulJ    15 年前

    您不能扩展一个由“something”索引的表,您只需使用它…

    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;
    /
    

    要使用扩展语法,本例应该这样做…

    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)

        2
  •  8
  •   Tony Andrews    15 年前

    如果不想使用关联数组(也称为表索引),那么请去掉“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
  •  8
  •   Rob van Laarhoven    13 年前

    不能扩展assositive数组。 只需给它赋值

    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;
    /
    
        4
  •  0
  •   kayakpim    8 年前

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

    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;