oracle 在 PL/SQL 中使用记录表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1606808/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 02:11:40  来源:igfitidea点击:

Using Tables of Records in PL/SQL

oracleplsql

提问by Thorsten

I've declared the following types in my PL/SQL package:

我在我的 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;

Then I declare a variable:

然后我声明一个变量:

obj t_obj_table;

However, when I want to use the variable, I cannot initialize or extend it:

但是,当我想使用该变量时,我无法对其进行初始化或扩展:

obj := t_obj_table ();

gives the following errror:

给出以下错误:

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

If I don't initialize it, I can't extend it to add some date as

如果我不初始化它,我就不能扩展它来添加一些日期

obj.EXTEND();

gives another error:

给出另一个错误:

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

How can I make this work?

我怎样才能使这项工作?

回答by PaulJ

You don't extend a table indexed by "something", you can just use it...

您不扩展由“某物”索引的表,您可以使用它...

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

To use the EXTEND syntax, this example should do it...

要使用 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;
/

Also see this link (Ask Tom)

另请参阅此链接(问汤姆)

回答by Rob van Laarhoven

You can not extend an assosiative array. Just assign values to it

您不能扩展关联数组。只需为它赋值

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

回答by Tony Andrews

If you don't want to use an associative array (aka index-by table) then leave out the "INDEX BY BINARY_INTEGER" clause. Your code then works OK:

如果您不想使用关联数组(又名索引表),则省略“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;

回答by kayakpim

Or just use a record ( or associate array of records )

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

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;