问题描述
我想创建一个内存数组变量,该变量可以在我的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.
这篇关于Oracle PL/SQL-如何创建一个简单的数组变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!