2014-02-08 3 views
1

개념은 다른 쿼리가 동일한 값을 가지고 있는지 또는 다른 쿼리에 존재하는지 여부를 확인하기 위해 2 개의 평등 한 테이블을 비교하려고합니다. 내가 테이블을 조인하는 방법은 다음과 같습니다. "a.sth, a.sth2, a.st3, b.value for table1 a, table2 b를 선택하십시오. a.key = b.valkey" 그러면 결과 값 table1의 열과 그 다음에 key2가 같은 table2의 열 value2가 있습니다. 이제 비슷한 데이터가 들어있는 다른 2 개의 테이블이 있는데 내 쿼리의 결과가 쿼리에 존재하는지 확인하려면 다음과 같이 다른 테이블을 작성하십시오 : "select a.sth, a.sth2, a.st3, b.value for table3 a, table4 b 여기서 a.key = b.valkey "오라클 비교 2 equijoined 테이블

내가 생각한 유일한 방법은 중첩 암시 적 커서를 사용하는 것이 었습니다. 예 :

BEGIN 
    FOR item IN (select a.sth, a.sth2, a.st3, b.value for table1 a, table2 b where a.key = b.valkey) 
    LOOP 
Begin 
FOR item2 IN (select a.sth, a.sth2, a.st3, b.value for table3 a, table4 b where a.key = b.valkey) 
    LOOP 
    Begin 
if (item1.sth = item2.sth) and (item1.sth2 = item2.sth2) and (item1.sth3 = item2.sth3) and (item1.value = item2.value) Then 
dbms_output.put_line("Found and value is the same"); 
Elsif (item1.sth = item2.sth) and (item1.sth2 = item2.sth2) and (item1.sth3 = item2.sth3) and Not (item1.value = item2.value) Then 
dbms_output.put_line("Found but value is different"); 
Exception When no_data_found then 
dbms_output.put_line("item1 was not found in table3"); 
End; 
    END LOOP; 
End; 
END LOOP; 
END; 

위의 내용은 내가 생각한 의사 코드입니다. 이런 식으로 할 수 있을까, 아니면 내가 사용할 수있는 더 나은 성능을 가진 대안이있을 수 있습니까? 나는 당신의 제안을 기다리고 있습니다.

답변

1

커서가 필요하지 않습니다. 일반적으로 set-based 코드는 더 잘 작동 할 것입니다. 쿼리는 다음과 같이 보일 것입니다 :

select coalesce(a.key, b.valkey) as thekey, 
     a.sth, a.sth2, a.st3, b.value 
     (case when a.key is null then 'Not found in a' 
      when b.key is null then 'Not found in b' 
      when (a.sth = b.sth) and (a.sth2 = b.sth2) and (a.sth3 = b.sth3) and 
        (a.value = b.value) 
      then 'Found and same' 
      when (a.sth = b.sth) and (a.sth2 = b.sth2) and (a.sth3 = b.sth3) and 
        (a.value <> b.value) 
      then 'Found and different' 
      else 'Other!!!' 
     end) 
from table1 a full outer join 
    table2 b 
    on a.key = b.valkey 
+0

미안하지만이게 어떻게 도움이 될 수 있는지 정확히 이해하지 못했습니다. 개념은 값에 따라 합쳐진 한 테이블에 2 개의 테이블이 있다는 것입니다. –

+0

@ GeorgeGeorgiou. . . 이 쿼리는 커서 대신 SQL을 사용한다는 점을 제외하면 PL/SQL 코드의 기능을 대부분 수행합니다. 그렇게하면 더 빨리 달릴 수 있습니다. –

+0

그러면 첫 번째 2 개 테이블 (1,2)의 조합 값이 다른 2 개 테이블 (3,4)에 있는지 확인하고 동일한 값을 가진 경우? 나는 설명서를 읽으려고했지만, 나는이 일이 어떻게 일어나는지 혼란스러워했다. –