Oracle PL/SQL 数组的索引是从 0 还是从 1?

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

Are Oracle PL/SQL arrays indexed from 0 or from 1?

oracleplsqloracle-apex

提问by Jacques Chester

I have in front of me a piece of code like this:

我面前有一段这样的代码:

FOR row IN 1..l_RowSet(1).count 
LOOP
   l_a_variable := l_RowSet(1)(row);
END LOOP;

l_RowSetis an ApEx type -- apex_plugin_util.t_column_value_list-- defined thus:

l_RowSet是 ApEx 类型 -- apex_plugin_util.t_column_value_list-- 定义如下:

type t_column_value_list  is table of wwv_flow_global.vc_arr2 index by pls_integer;

where wwv_flow_global.vc_arr2is defined as

其中wwv_flow_global.vc_arr2定义为

type vc_arr2 is table of varchar2(32767) index by binary_integer;

The vc_arr2is passed back to my code from the apex_plugin_util.get_datafunction. The vc_arr2 is indexed by columnnumber, not by row.

vc_arr2被传递回从我的代码apex_plugin_util.get_data功能。vc_arr2 按号索引,而不是按行索引。

As best I can make out this means that the data is effectively stored in a 2D array, indexed by column and then by row.

尽我所能,这意味着数据有效地存储在二维数组中,按列索引,然后按行索引。

When using the LOOP statement, would this be indexed from zero or from one? Because it seems to me that I ought to be able to make that LOOP redundant, ie:

使用 LOOP 语句时,是从 0 还是从 1 索引?因为在我看来,我应该能够使该 LOOP 变得多余,即:

l_a_variable := l_RowSet(1)(1);

But I'd need to know in advance whether to give 0 or 1 as the initial row.

但我需要提前知道是将 0 还是 1 作为初始行。

I can't find a clear answer in the Oracle docs (unsurprisingly, "index" is a fairly widely-used term) and a look through SO doesn't show anybody else with the same question either.

我在 Oracle 文档中找不到明确的答案(不出所料,“索引”是一个相当广泛使用的术语),并且通过 SO 浏览也没有发现其他任何人也有相同的问题。

采纳答案by Justin Cave

An associative array isn't necessarily dense. There may be an element at index 0, there may be an element at index -1, there may be an element at index 1. Or you might have elements at indexes 17, 42, and 127. The code you posted implies that the associative array is dense and that the indexes start at 1.

关联数组不一定是密集的。索引 0 处可能有一个元素,索引 -1 处可能有一个元素,索引 1 处可能有一个元素。或者您可能在索引 17、42 和 127 处有元素。您发布的代码暗示关联数组是密集的,索引从 1 开始。

In the specific case of apex_plugin_util.get_datathe collection should be dense and should start at 1. If the loop is actually not doing anything other than what you posted, you could replace it by fetching the last element of l_RowSet(1), i.e.

在特定情况下,apex_plugin_util.get_data集合应该是密集的,并且应该从 1 开始。如果循环实际上除了您发布的内容之外没有做任何事情,您可以通过获取 的最后一个元素来替换它l_RowSet(1),即

l_a_variable := l_RowSet(1)(l_RowSet(1).count);