access to child variable pl sql

Viewed 98

I am trying to access the child variable of the object through the parent

declare
  i a22;
  i1 VARCHAR(10);
begin
  i := a22('a22', t_b1(
                       b22('b22', t_a1(a22('a22_2', t_b1(b22('b22_2', t_a1())))))
                       )


           );

   dbms_output.put_line('i.name_a1='||i.name_a1);
   dbms_output.put_line('i.t_b1(1).name_b1='||i.o_b(1).name_b1);
   dbms_output.put_line('i.o_b(1).o_a(1).name_a1='||i.o_b(1).o_a(1).name_a1);
end;
/

When compiling, an error occurs indicating that the variable needs to be declared. It is logical that the parent does not know about the variables of the child object. But when initializing everything successfully. How can I access a child variable?

The objects themselves:

Create type a1 is object (
  name_a1 varchar2(10)
) NOT FINAL NOT INSTANTIABLE;

Create type b1 is object (
  name_b1 varchar2(10)
) NOT FINAL NOT INSTANTIABLE;

create type t_a1 is table of a1;
create type t_b1 is table of b1;

Create type b22 UNDER b1 (
  o_a t_a1
) INSTANTIABLE;

Create type a22 UNDER a1 (
  o_b t_b1
) INSTANTIABLE;
1 Answers

you can use the TREAT function to access attributes of object subtypes.

Since o_b is of the type table of b1 you have to treat the object o_b(1) as b22 because only b22 object contains the member of type table of a1

dbms_output.put_line('i.o_b(1).o_a(1).name_a1='||treat(i.o_b(1) as b22).o_a(1).name_a1);

Result:

dbms_output:
i.o_b(1).o_a(1).name_a1=a22_2

db<>fiddle here

Related