2012-11-10 38 views
4

我读了约IS OF TYPE,我预计它应该返回TRUE,FALSE或NULL。IS OF TYPE产生一个异常

我有两个对象类型:

CREATE TYPE o1 AS OBJECT (id NUMBER); 
/
CREATE TYPE o2 AS OBJECT (id NUMBER); 
/

当我运行下面的代码,一切正常。

DECLARE 
    type1 o1; 
BEGIN 
    type1 := o1(id=>1); 

    if (type1 IS OF (o1)) then 
     DBMS_OUTPUT.PUT_LINE('type1 is o1');      
    END if; 
END; 
/

但是当我尝试运行:

DECLARE 
    type1 o1; 
BEGIN 
    type1 := o1(id=>1); 

    if (type1 IS OF (o2)) then 
     DBMS_OUTPUT.PUT_LINE('type1 is o1');      
    END if; 
END; 
/

我收到了以下异常

Error report: 
ORA-06550: line 6, column 21: 
PLS-00382: expression is of wrong type 
ORA-06550: line 6, column 4: 
PL/SQL: Statement ignored 
06550. 00000 - "line %s, column %s:\n%s" 
*Cause: Usually a PL/SQL compilation error. 
*Action: 

在本文档中没有明确的解释,我应该赶上例外,如果事情是错误的类型?或者,我是否应该在IF条件下期望错误?

回答

4

如果你已经宣布你为O1类型的变量,那么你可以使用is of [type]条件来测试你的唯一变量是否o1类型或者是o1的亚型。下面是一个例子(变量必须被实例化):

-- 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 

更新

Take a look at this获得更多有关is of[type]。通常在编译时已知变量的数据类型,但如果必须处理动态类型,则可以查看anydata(对象数据类型)。这里有一个简单的例子:

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 
+0

谢谢(我知道 - 但想检查任何类型),对我来说很奇怪,在这种情况下,文档不太清楚。有没有其他的选择(我知道可以投到任何一个然后获取名称,可以与user_types视图进行比较)? – reichel

+0

我已经更新了答案。 –

+0

是的,我写了关于这个解决方案,但我认为有一些像IS OF一样简单。如果有人知道其他解决方案,请写下,这(半)解决我的问题。谢谢。 – reichel