Arrays 声明时VARRAY中的Oracle PL/SQL默认值
在声明下一行代码时,元素中的初始值是多少Arrays 声明时VARRAY中的Oracle PL/SQL默认值,arrays,oracle,Arrays,Oracle,在声明下一行代码时,元素中的初始值是多少 TYPE IntA IS VARRAY(100) of NUMBER; list IntA := IntA(); 我想知道列表元素中存储的默认值。您似乎误解了。当您将类型声明为VARRAY(100)时,您指定的是该类型的实例可以包含的最大元素数,而不是它将包含的元素数 在你的例子中,你是: 如果参数列表为空,则构造函数返回空集合 所以集合是空的;它有零个元素(您设置的最大值为100);而first和last方法返回null,因为它们对于空集合是未知的/
TYPE IntA IS VARRAY(100) of NUMBER;
list IntA := IntA();
我想知道列表元素中存储的默认值。您似乎误解了。当您将类型声明为
VARRAY(100)
时,您指定的是该类型的实例可以包含的最大元素数,而不是它将包含的元素数
在你的例子中,你是:
如果参数列表为空,则构造函数返回空集合
所以集合是空的;它有零个元素(您设置的最大值为100);而first
和last
方法返回null,因为它们对于空集合是未知的/无意义的
当您使用extend
将元素添加到数组中时,新元素也将为null,直到您为它们赋值为止
为了说明@Motor在注释中描述的行为,您可以使用匿名块操纵数组,并查看每个阶段的外观:
set serveroutput on
declare
type t_array is varray(100) of number;
l_array t_array;
procedure show_array_info(p_step pls_integer) is
begin
dbms_output.put('Step ' || p_step || ': ');
if l_array is null then
dbms_output.put_line('array has not been intialised');
return;
end if;
dbms_output.put_line('count ' || l_array.count
|| ' first ' || nvl(to_char(l_array.first), '(null)')
|| ' last ' || nvl(to_char(l_array.last), '(null)'));
if l_array.count = 0 then
return;
end if;
for i in l_array.first..l_array.last loop
dbms_output.put_line(' element ' || i || ': '
|| nvl(to_char(l_array(i)), '(null)'));
end loop;
end show_array_info;
begin
-- array has been declared buyt not initialised
show_array_info(1);
-- initialisae array as empty
l_array := t_array();
show_array_info(2);
-- increase number of elements by default 1; element is null
l_array.extend;
show_array_info(3);
-- set value for last element
l_array(l_array.count) := 42;
show_array_info(4);
-- increase number of elements by 3; new elements are null
l_array.extend(3);
show_array_info(5);
-- set value for last element; others remain null
l_array(l_array.count) := 17;
show_array_info(6);
-- remove last two elements; other two remain
l_array.trim(2);
show_array_info(7);
-- remove all elements, leaving array empty
l_array.trim(l_array.count);
show_array_info(8);
-- l_array.extend(97);
end;
/
这将产生:
PL/SQL procedure successfully completed.
Step 1: array has not been intialised
Step 2: count 0 first (null) last (null)
Step 3: count 1 first 1 last 1
element 1: (null)
Step 4: count 1 first 1 last 1
element 1: 42
Step 5: count 4 first 1 last 4
element 1: 42
element 2: (null)
element 3: (null)
element 4: (null)
Step 6: count 4 first 1 last 4
element 1: 42
element 2: (null)
element 3: (null)
element 4: 17
Step 7: count 2 first 1 last 2
element 1: 42
element 2: (null)
Step 8: count 0 first (null) last (null)
如果在集合为空时尝试引用第一个元素,或者更一般地说,引用任何使用大于当前计数的数字的元素,则会出现错误
ORA-06533:Subscript beyond count
。如果您试图将集合扩展到超出其声明的限制,您将获得ORA-06532:超出限制的下标
What的初始值是多少。您正在这里声明一个类型。变量声明在哪里?@motor,数字数组元素的初始值。在Java中,int数组中的元素都初始化为0,这种情况如何?您在这里声明的是空数组。java中空数组的初始值是多少?@NicholasKrasnov在这种情况下这是错误的。列表(1)不存在且列表不为空。列表为空数组。tohave值应首先使用EXTEND或BULK COLLECT(它实际上创建了新数组),最后将返回NULL。没有第一个和最后一个元素,因为数组为空。计数返回0。感谢您的明确解释。我很少在sql中处理数组。所以当我读到这本书时,我感到好奇。然而,我觉得oracle的文档相当模糊。