If you have declared your variable as O1
type then you can use is of [type]
condition to test only whether your variable is of o1
type or is of o1
's subtype. Here is an example(variables must be instantiated):
-- base type
SQL> create or replace type o1 as object(
2 prop number
3 )not final;
4 /
Type created
-- O1's subtype
SQL> create or replace type o2 under o1(
2 prop1 number
3 );
4 /
-- test if the l_o1 is of O1 type
SQL> declare
2 l_o1 o1;
3 begin
4 l_o1 := o1(prop=>1);
5 if l_o1 is of (o1)
6 then
7 dbms_output.put_line('Yes');
8 else
9 dbms_output.put_line('No');
10 end if;
11 end;
12 /
Yes
PL/SQL procedure successfully completed
-- test if the l_o1 is of O2 type
SQL> declare
2 l_o1 o1;
3 begin
4 l_o1 := o1(prop=>1);
5 if l_o1 is of (o2)
6 then
7 dbms_output.put_line('Yes');
8 else
9 dbms_output.put_line('No');
10 end if;
11 end;
12 /
No
PL/SQL procedure successfully completed
-- test if the l_o2 is of O2 type
SQL> declare
2 l_o2 o2;
3 begin
4 l_o2 := o2(prop=>1, prop1 => 1);
5 if l_o2 is of (o2)
6 then
7 dbms_output.put_line('Yes');
8 else
9 dbms_output.put_line('No');
10 end if;
11 end;
12 /
Yes
PL/SQL procedure successfully completed
Update:
Take a look at this to get more information about is of[type]
. Usually data type of a variable is known at compile time, but if you have to deal with dynamic typing you may look at anydata
(object data type). Here is a simple example:
SQL> declare
2 l_o1 o1;
3
4 -- Here is a procedure(for the sake of simplicity has not
5 -- been written as a schema object)
6 -- that doesn't not know
7 -- variable of what dada type will be passed in
8 -- at compile time;
9 procedure DoSomething(p_var anydata)
10 is
11 begin
12 case p_var.gettypename
13 when 'HR.O1'
14 then dbms_output.put_line('O1 data type. Do something');
15 when 'HR.O2'
16 then dbms_output.put_line('O2 data type. Do something');
17 else
18 dbms_output.put_line('Unknown data type');
19 end case;
20 end;
21
22 begin
23 l_o1 := o1(prop => 1);
24 DoSomething(anydata.ConvertObject(l_o1));
25 end;
26 /
O1 data type. Do something
PL/SQL procedure successfully completed