collection 方法是一个内置的 pl/sql 子程序,可以返回 collection 信息,或是在 collection 上执行操作,很方便。你可以通过点记
本文内容
exists 方法 count 方法 limit 方法 first 和 last 方法 prior 和 next 方法 extend 方法 trim 方法 delete 方法
collection 方法是一个内置的 pl/sql 子程序,可以返回 collection 信息,或是在 collection 上执行操作,很方便。
你可以通过点记号来调用 collection 方法。语法如下图所示:
图1 collection method 调用
不能在 sql 语句调用 collection 方法。
当 collection 为空时,你只能使用 exists 方法,使用其他方法都会抛出 collection_is_null 异常。
exists 方法若 collection 中第 n 个元素存在,则 exists(n) 返回 true;否则,返回 false。exists 方法结合 delete 方法,会把 collection 变成稀疏 nested tables(sparse nested tables)。通过 exists 方法,避免引用一个不存在的元素,从而产生异常。当传递一个超出范围的标值时,exists 方法返回 false,而不是产生 subscript_outside_limit 异常。
示例1:演示检查元素是否存在
; n numlist := numlist(1,3,5,7);dbms_output.put_line(dbms_output.put_line('ok, element #99 does not exist at all.'); end if;end;/
count 方法count 返回 collection 中元素的当前数量。当你不知道 collection 中有多少元素时,很有用。例如,当你把获取的表的一列,放到一个 nested table 时,元素的数量取决于结果集的大小。
对于 varray,count 总是等于 last。通过 extend 和 trim 方法,你可以增加或减少 varray 的大小,,因此,count 值是变化的,取决于 limit 方法的值。
对于 nested tables,count 方法通常等于 last 方法。然而,若你从 nested table 删除元素,则 count 小于 last。当你整理元素时,count 会忽略已删除的元素。使用不带参数的 delete 方法会设置 count 为 0。
备注:first 方法和 last 方法返回最大和最小的索引数。后面说明。