引用未初始化的集合错误

时间:2018-02-15 11:53:04

标签: oracle plsql

create or replace function cmp return number as

    type GyomuAuth1D is table of char(2);
    type GyomuAut2D_type is table of GyomuAuth1D;
    GyomuAut2D GyomuAut2D_type;   

begin
    for x in 1..50
    loop
        GyomuAut2D(x)(0) := null; 
    end loop;
end;

我正在尝试使用for循环将空值分配给2D数组的前50个位置。 但它正在显示"参考未初始化的集合"错误 任何人都可以发现我做了什么错误...提前致谢

1 个答案:

答案 0 :(得分:2)

First you need to initialise GyomuAut2D. It is still empty though, so there is no element 1 until you extend it (otherwise you'll get ORA-06533: Subscript beyond count). I've chosen to extend(50) once for efficiency since we know how many we want, although you could also extend once per loop iteration.

The same applies to each GyomuAut2D(x) as it is also an uninitialised and empty collection.

Also the function needs to return something.

declare
    function cmp
        return number
    is
        type GyomuAuth1D is table of varchar2(2);
        type GyomuAut2D_type is table of GyomuAuth1D;
        GyomuAut2D GyomuAut2D_type := GyomuAut2D_type();
    begin
        GyomuAut2D.extend(50);

        for x in 1..50
        loop
            GyomuAut2D(x) := GyomuAuth1D(null);
        end loop;

        return GyomuAut2D.count;
    end;
begin
    dbms_output.put_line(cmp);
end;