Oracle PL/SQL - 如何创建一个简单的数组变量?

2021-12-06 00:00:00 oracle plsql oracle9i

我想创建一个可在我的 PL/SQL 代码中使用的内存中数组变量.我在 Oracle PL/SQL 中找不到任何使用纯内存的集合,它们似乎都与表相关联.我希望在我的 PL/SQL(C# 语法)中做这样的事情:

I'd like to create an in-memory array variable that can be used in my PL/SQL code. I can't find any collections in Oracle PL/SQL that uses pure memory, they all seem to be associated with tables. I'm looking to do something like this in my PL/SQL (C# syntax):

string[] arrayvalues = new string[3] {"Matt", "Joanne", "Robert"};

甲骨文:9i

推荐答案

您可以将 VARRAY 用于固定大小的数组:

You can use VARRAY for a fixed-size array:

declare
   type array_t is varray(3) of varchar2(10);
   array array_t := array_t('Matt', 'Joanne', 'Robert');
begin
   for i in 1..array.count loop
       dbms_output.put_line(array(i));
   end loop;
end;

对于无界数组或 TABLE:

Or TABLE for an unbounded array:

...
   type array_t is table of varchar2(10);
...

这里的表"一词与数据库表无关,令人困惑.两种方法都创建内存数组.

The word "table" here has nothing to do with database tables, confusingly. Both methods create in-memory arrays.

使用其中任何一个,您都需要在添加元素之前初始化和扩展集合:

With either of these you need to both initialise and extend the collection before adding elements:

declare
   type array_t is varray(3) of varchar2(10);
   array array_t := array_t(); -- Initialise it
begin
   for i in 1..3 loop
      array.extend(); -- Extend it
      array(i) := 'x';
   end loop;
end;

第一个索引是 1 而不是 0.

The first index is 1 not 0.

相关文章