我有两种对象类型:
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 typeORA-06550: line 6,column 4:PL/sql: Statement ignored06550. 00000 - "line %s,column %s:\n%s"*Cause: Usually a PL/sql compilation error.*Action:
在文档中没有明确的解释,如果某些类型错误,我是否应该捕获异常?或者,我应该在IF条件下假设错误吗?
解决方法 如果您已将变量声明为O1类型,那么您可以使用[type]条件来测试您的变量是o1类型还是o1的子类型.这是一个例子(变量必须实例化):-- base type sql> create or replace type o1 as object( 2 prop number 3 )not final; 4 /Type created-- O1's subtypesql> create or replace type o2 under o1( 2 prop1 number 3 ); 4 /-- test if the l_o1 is of O1 typesql> 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 /YesPL/sql procedure successfully completed-- test if the l_o1 is of O2 typesql> 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 /NoPL/sql procedure successfully completed-- test if the l_o2 is of O2 typesql> 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 /YesPL/sql procedure successfully completed
更新:
Take a look at this获取有关[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 somethingPL/sql procedure successfully completed总结
以上是内存溢出为你收集整理的oracle – IS OF TYPE生成异常全部内容,希望文章能够帮你解决oracle – IS OF TYPE生成异常所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)