2013-04-24 4 views
5

테이블 변수가 없기 때문에 (Oracle SQL Server와 같이) Oracle 코드 블록에서 콜렉션을 사용하고 있습니다.where 절에있는 Oracle 콜렉션

DECLARE 
    TYPE I_NAME IS TABLE OF NVARCHAR2(512);  
    I_ITEMNAME  I_NAME := I_NAME(); 
BEGIN 

"BULK COLLECT INTO I_ITEMNAME"을 사용하여 컬렉션을 채우고 있습니다.
SELECT 쿼리의 WHERE 절에서이 컬렉션을 사용하려고하지만이를 수행 할 방법을 찾을 수 없습니다. 현재 나는 루프를 사용하여 하나씩 아이템을 얻는다.
어떻게 I_ITEMNAME IN COL TBL FROM

SELECT * 같은 WHERE 절 뭔가 직접 수집을 사용할 수 있습니까?

, 감사합니다

답변

9

당신은 SQL 절에 로컬로 선언 모음을 사용할 수 없습니다 : SQL에 대한 알 수 있도록이 본질적으로, 스키마 레벨에서 선언 않다면

declare 
    type i_name is table of nvarchar2(512); 
    i_itemname i_name := i_name(); 
    c number; 
begin 
    select distinct owner bulk collect into i_itemname from all_objects; 
    dbms_output.put_line(i_itemname.count); 
    select count(*) into c 
    from all_tables 
    where owner in (select * from table(i_itemname)); 
    dbms_output.put_line(c); 
end; 
/

    where owner in (select * from table(i_itemname)); 
             * 
ERROR at line 10: 
ORA-06550: line 10, column 41: 
PLS-00642: local collection types not allowed in SQL statements 
ORA-06550: line 10, column 35: 
PL/SQL: ORA-22905: cannot access rows from a non-nested table item 
ORA-06550: line 8, column 5: 
PL/SQL: SQL Statement ignored 

그러나 당신이 할 수있는 유형뿐만 아니라 PL/SQL :

create type i_name is table of nvarchar2(512); 
/

Type created. 

declare 
    i_itemname i_name := i_name();  
    c number; 
begin 
    select distinct owner bulk collect into i_itemname from all_objects; 
    dbms_output.put_line(i_itemname.count); 
    select count(*) into c from all_tables 
    where owner in (select * from table(i_itemname)); 
    dbms_output.put_line(c); 
end; 
/

No errors. 
18 
128 

PL/SQL procedure successfully completed. 

는 또한 table 구조에 참여보다는 스와을 사용할 수 있습니다 bquery :

... 
    select count(*) into c 
    from table(i_itemname) t 
    join all_tables at on at.owner = t.column_value; 
... 

나는 당신이 무엇을하고 있는지 분명하지 않습니다. (컬렉션을 다른 용도로 사용하지 않는다면, 원시 데이터에 합류하는 것이 더 나을 것입니다. 그러나 컬렉션이 이유가 있다고 가정합니다). @haki 댓글에서 언급 한 바와 같이


, 당신도 할 수 있습니다 : 한 i_name으로하고 열은 are the same type과 비교하고

... 
    select count(*) into c 
    from all_tables 
    where owner member of (i_itemname); 
... 

.... 내 예제에서는 nvarchar2varchar2과 비교하려고 시도했기 때문에 0 행을 찾았지만 i_namevarchar2(512)으로 재정의하면 일치하는 항목을 찾습니다. 귀하의 경우 아마도 tab.col은 어쨌든 nvarchar2입니다.

+2

+1 당신이 (i_itemname)의 owner member 인 all_tables에서 'select count (*)'와 같이 사용할 수도 있다고 생각합니다. – haki

+0

@haki - 좋았어. 나는 그걸 사용 해본 적이 없다고 생각한다. 내 예제에서는 일치하지 않습니다 ([유형이 일치하지 않기 때문에]) (http://docs.oracle.com/cd/E11882_01/server.112/e26088/conditions006.htm#sthref1966) - 'i_type'을 as 'nvarchar2'하지만'owner'는'varchar2'이지만'i_type'이 다시 정의되면 깔끔합니다. (나는 그것을 나의 대답에 추가했다. 그러나 만일 당신이 그것과 함께 당신 자신의 대답을 게시하면 나는 그것을 제거 할 것이고, 신용을 얻으 려하지 않는다!) –

관련 문제