솔직한 평등 테스트는 중첩 된 테이블에 대해 작업 :
SQL> declare
2 type nt is table of number;
3 nt1 nt;
4 nt2 nt;
5 nt3 nt;
6 begin
7 nt1 := nt(1,2,3);
8 nt2 := nt(1,2,3);
9 if nt1 = nt2 then
10 dbms_output.put_line('NT2 is the same nested table as NT1');
11 else
12 dbms_output.put_line('NT2 is a different nested table from NT1');
13 end if;
14 nt2 := nt(1,2,3,4);
15 if nt1 = nt3 then
16 dbms_output.put_line('NT3 is the same nested table as NT1');
17 else
18 dbms_output.put_line('E3 is a different nested table from NT1');
19 end if;
20 end;
21/
NT2 is the same nested table as NT1
E3 is a different nested table from NT1
PL/SQL procedure successfully completed.
SQL>
을 동일-전체의 개체의 사실이 아니다 그러나 :
SQL> create or replace type new_emp as object (
2 ename varchar2(10)
3 , sal number
4 , deptno number
5 , job varchar2(10))
6/
Type created.
SQL> declare
2 e1 new_emp;
3 e2 new_emp;
4 begin
5 e1 := new_emp('KESTELYN', 3700, 30, 'MARKETING');
6 e2 := new_emp('KESTELYN', 3700, 30, 'MARKETING');
7 if e1 = e2 then
8 dbms_output.put_line('E2 is the same as E1');
9 else
10 dbms_output.put_line('E2 is different from E1');
11 end if;
12 end;
13/
if e1 = e2 then
*
ERROR at line 7:
ORA-06550: line 7, column 11:
PLS-00526: A MAP or ORDER function is required for comparing objects in PL/SQL.
SQL>
우리는 명시 적으로 비교를 실행하기위한 멤버 함수를 정의 할 필요가있다. 그래서 여기에 MAP 기능을 가진 동일한 객체가 있습니다. 예제 구현은 해시 된 문자열을 생성합니다.이 값은 나중에 비교할 값을 저장하려는 경우 유용하지만 연결 문자열 만 반환 할 수 있습니다 (특히 DBMS_CRYPTO의 EXECUTE는 기본적으로 부여되지 않습니다). NVL() 함수는 (null, value) 및 (value, null)이 동일한 것으로 평가되는 것을 피하기 위해 필요합니다. 마법 값을 사용할 때 항상 위험이 있으므로 신중하게 선택해야합니다. 오라클은 기본적으로이 작업을 수행하지 않는 이유 당신은 궁금 할 것이다
SQL> declare
2 e1 new_emp;
3 e2 new_emp;
4 begin
5 e1 := new_emp('KESTELYN', 3700, 30, 'MARKETING');
6 e2 := new_emp('KESTELYN', 3700, 30, 'MARKETING');
7 if e1 = e2 then
8 dbms_output.put_line('E2 is the same as E1');
9 else
10 dbms_output.put_line('E2 is different from E1');
11 end if;
12 end;
13/
E2 is the same as E1
PL/SQL procedure successfully completed.
SQL>
:
SQL> create or replace type new_emp as object (
2 ename varchar2(10)
3 , sal number
4 , deptno number
5 , job varchar2(10)
6 , map member function equals return raw)
7/
Type created.
SQL> create or replace type body new_emp as
2 map member function equals return raw
3 is
4 begin
5 return dbms_crypto.hash(
6 utl_raw.cast_to_raw(nvl(self.ename,'***')||
7 nvl(self.sal,-99)||
8 nvl(self.deptno,-99)||
9 nvl(self.job,'***')
10 )
11 , 1);
12 end equals;
13 end;
14/
Type body created.
SQL>
이제 우리는 우리의 객체의 인스턴스를 비교하기위한 기준이있다. 글쎄, TYPE 구현은 하나의 비교 메소드 만 허용합니다 (우리가 MAP 함수를 가지고 있다면 ORDER 함수를 가질 수 없습니다). 그래서 우리는 우리 자신의 평등 정의를 선택할 수있는 능력이 필요합니다. 예를 들어,
rectangle
이라는 유형은 을 리턴하는
area()
이라는 MAP 기능을 가질 수 있습니다.