2011-10-05 4 views
1

여러 커서에서 하나씩 추가하여 사용자 지정 개체를 포함하는 중첩 테이블을 만들고 싶습니다. 하지만 나는 테이블에 중복 된 것을 원하지 않습니다. 이것을 어떻게 할 수 있습니까? 여기요소가없는 경우에만 중첩 테이블에 요소를 삽입 할 수 있습니까?

내가 테이블에 요소를 추가하는 방법입니다 : 이것은 단지 예이며,이 특별한 경우에 선택은 단 하나의 커서를하기 위해 변경 될 수 있습니다

create type recipient as object (firstname varchar2, lastname varchar2, email varchar2); 

declare type recipients_list is table of recipient; 
rec recipients_list := recipients_list(); 

cursor admins is 
    select firstname, lastname, email 
    from users 
    where profile_id = 1; 

cursor operators is 
    select firstname, lastname, email 
    from users 
    where operator = 1; 

-- an user may be both admin and operator 

.... 

for to_email in admins 
loop 
    rec.extend; 
    rec(rec.last) := recipient(to_email.firstname, to_email.lastname, to_email.email); 
end loop; 

for to_email in operators 
loop 
    rec.extend; 
    rec(rec.last) := recipient(to_email.firstname, to_email.lastname, to_email.email); 
end loop; 

하는 것으로. 하지만 제 신청서에는 상황이 다릅니다. 감사!

답변

5

Oracle 10g 이상을 사용하는 경우이 문제를 해결하는 몇 가지 쉬운 방법이 있습니다.

하나는 기존 코드를 사용하고, OPERATORS 루프에서는 the MEMBER OF operator을 사용하여 컬렉션에 이미 있는지 테스트합니다.

그러나 더 간단한 방법은 세 가지 모음을 정의하는 것입니다. 대량 수집을 사용하여 각 쿼리의 전체 레코드 집합으로 두 개의 컬렉션을 채운 다음 세 번째 컬렉션을 사용하여 중복 된 레코드를 필터링합니다. 이 같은

Soemthing :

declare 

    type recipients_list is table of recipient; 
    recs recipients_list := recipients_list(); 
    admins recipients_list := recipients_list(); 
    operators recipients_list := recipients_list(); 

begin 

    select firstname, lastname, email 
    bulk collect into admins 
    from users 
    where profile_id = 1; 

    select firstname, lastname, email 
    bulk collect into operators 
    from users 
    where operator = 1; 

    recs := admins multiset union distinct operators; 

end; 
/

"이 가능 여러 조합을 가지고?"

을 그것은 특정 가능하다. 최대 값이 있는지 여부는 알 수 없지만 높은 값이 있을지는 확실합니다. 필자는 오라클의 한계가 관대 한 편이라고 생각합니다. 우리가 한계에 맞서 자신을 찾으면 아마도 그것이 무엇이든하는 더 좋은 방법 일 것입니다.

+0

감사 APC! 여러 개의 노동 조합을 가질 수 있습니까? 새로운 select를 추가하면'recs : = admins multiset union distinct operators multiset union distinct vips; '와 같이 쓸 수 있습니까? 고맙습니다! – radonys

관련 문제