2016-11-30 8 views
3

Oracle 11gR2では、単純なPL/SQLオブジェクト型を作成しました。平等/不平等のための2つのインスタンスを比較しようとすると、私はPLS-00526: A MAP or ORDER function is required for comparing objects in PL/SQLエラーを取得し、Oracle documentationは明らかに、私はエラーを再現するために使用ここで「PLS-00526:MAPまたはORDER関数が必要です」オブジェクトが等しいかどうか比較する

If neither a MAP nor an ORDER method is specified, then only comparisons for equality or inequality can be performed.

は、PL/SQLコードの例であると述べている場合でも:

create or replace type point_t is object (x number, y number); 
/

declare 
    p1 point_t := point_t(1, 2); 
    p2 point_t := point_t(1, 2); 
    p3 point_t := point_t(2, 1); 
begin 
    dbms_output.put_line('p1 = p1 ' || case when p1 = p1 then 'OK' else 'FAIL' end); 
    dbms_output.put_line('p2 = p1 ' || case when p2 = p1 then 'OK' else 'FAIL' end); 
    dbms_output.put_line('p3 <> p1 ' || case when p3 <> p1 then 'OK' else 'FAIL' end); 
end; 
/
+0

興味深い。この動作はドキュメントと矛盾しているようです。 –

答えて

2

はい、どちらもMAPでもORDERも指定されていない場合は、オブジェクトを同等か不等式かを比較できますが、PL/SQLブロックではなくSQL文でのみ比較できます。

Type created. 

CMP_RES 
------- 
OK  

1 row selected. 

p1 = p2 : FAIL 
PL/SQL procedure successfully completed. 

しかし、PL/SQLブロック内のオブジェクトを直接比較する必要がある場合は、(オブジェクトの比較ルールを定義する必要があります。Database Object-Relational Developer's Guide

if you do not declare one of these methods, you can only compare objects in SQL statements, and only for equality or inequality.

create or replace type point_t is object (x number, y number); 
/

select case 
     when point_t(1,1) = point_t(1,1) then 'OK' 
     else 'FAIL' 
     end as cmp_res 
    from dual; 


set serveroutput on; 
declare 
    l_p1 point_t := point_t(1,2); 
    l_p2 point_t := point_t(1,2); 
    l_res varchar2(7) := 'OK'; 
begin 
    select 'FAIL' 
    into l_res 
    from dual 
    where l_p1 != l_p2; -- put it in the where clause just for the sake 
         -- of demonstration. Can do comparison in the 
         -- select list as well. 
    dbms_output.put_line('p1 = p2 : ' || l_res); 
end; 

結果から

見積もり1つのオブジェクトは等しい/不等であり、他のオブジェクトよりも大きいかまたは小さい、特にオブジェクトが多くのプロパティを有する場合)MAPまたはORDERの方法を実装する必要があります。

関連する問題