【问题标题】:Oracle PLSQL nested table iteration via index - can indices be out of order?Oracle PLSQL 通过索引进行嵌套表迭代 - 索引可以乱序吗?
【发布时间】:2021-05-02 12:44:34
【问题描述】:

我一直在不同的 oracle.com 网站上阅读,

FOR i IN nested_table.FIRST .. nested_table.LAST

这是您在 PLSQL 中通常执行的操作,当迭代嵌套表类型的所有元素时(只要没有删除任何元素)。

我的嵌套表的存在方式是通过做

type  nested_table_type  is table of  varchar2(20)

在不同的包中

nested_table   other_package.nested_table_type := other_package.nested_table_type();

然后,在一个循环中

nested_table.extend;
nested_table(nested_table.last) := something;

任意次数。 然后,我想对每个值做一些事情,就像在其他语言中使用 for each 一样。我可以在这里使用 for 循环吗?有人告诉我要小心,因为在 Oracle 的情况下索引可能不按顺序排列,因此 for 循环可能不会考虑某些索引。我绝对应该使用这个,他说:

index := nested_table.first;
while (index is not null)
loop
   do things...
   index := nested_table.next(index);
end loop;

这是真的吗?索引如何不按顺序排列,或者 for 循环不会全部遍历它们?

感谢您的帮助:)

编辑:

这很可能是某种误解。我按原样留下了代码。不过,感谢您阅读/回答,希望这对将来的某人有所帮助:)

【问题讨论】:

  • 嵌套表 SQL 列是无序的,但嵌套表 PL/SQL 变量是有序的。在文档中,您可以阅读Understanding Nested Tables 和 NEXT 方法的描述以了解更多详细信息。
  • @kfinity 这可能是我朋友所说的,我一定误解了他。非常感谢! :)

标签: oracle for-loop indexing plsql nested-table


【解决方案1】:

索引是有序的,只是你可以创建稀疏表,这意味着一些索引可能会丢失。

但是,对于您的情况,使用 i IN t.FIRST .. t.LAST 非常好。

【讨论】:

    猜你喜欢
    • 2021-09-07
    • 2016-04-05
    • 2018-10-01
    • 2021-07-18
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    相关资源
    最近更新 更多