Search code examples
oracleplsqltypescollections

Got PLS-00382: expression is of wrong type for oracle collection


I want to populate collection with integers but got error

In package spec:

type rec_int is record ( i integer );
      
type typ_int is table of rec_int;
    

In package body

    function test_randset return pls_integer is
      int_array typ_int;
      ret pls_integer;
    begin
      
      int_array(1) := 1;

      ret := randset(int_array);
    
      return ret;
    end; 

Error:

Error: PLS-00382: expression is of wrong type
Line: 50
Text: int_array(1) := 1;

Error: PL/SQL: Statement ignored
Line: 50
Text: int_array(1) := 1;

Solution

  • int_array is a typ_int, which is a record with an attribute named i, so the assignment should be

    int_array(1).i := 1;
    

    instead of this:

    int_array(1) := 1;
    

    If you defined type_int as simply

    type typ_int is table of integer;
    

    then your original assignment would work. I would however give the type a more meaningful name such as integer_ntt.

    You can test this kind of thing in an anonymous block without having to create a package, for example:

    declare
        --type rec_int is record(i integer);
    
        type integer_tt is table of integer;
    
        function test_randset return pls_integer
        is
            int_array  integer_tt;
            ret        pls_integer;
        begin
            int_array(1) := 1;
            --ret := randset(int_array);
            return ret;
        end;
    begin
        null;
    end;