2014-02-11 2 views
0

커서를 사용하여 table1의 두 column1, column2에서 값을 읽으려고합니다. 그런 다음이 값을 다른 커서에 전달하거나 문을 선택하여 내 PL/SQL 스크립트가이 두 열의 값을 사용하여 table2라는 다른 테이블에서 데이터를 가져옵니다.커서 값을 변수에 전달하는 방법은 무엇입니까?

가능합니까? 그리고 그런 일을하는 가장 빠르고 가장 빠른 방법은 무엇입니까?

감사 :

+1

refcursors이위한 하위 쿼리를 사용하는 것이 더 효율적이다, 하지만 난 항상, 같은 하위 쿼리를 수행 할 수 있습니다 .. 또한 당신은 중첩 테이블을 사용하여 고려해 볼 수 있습니다. (PL SQL 테이블). 중첩 테이블을 실제 테이블로 캐스팅하고 쿼리에 사용할 수 있습니다. –

답변

0

예, 커서 값을 변수에 전달할 수 있습니다. fetch <cursor_name> into <variable_list>을 사용하면 커서에서 하나 이상의 행을 가져올 수 있습니다. 그런 다음 where 절의 변수를 select into 문으로 사용할 수 있습니다. 당신이 암시 적 커서를 사용하는 경우 예,

declare 
    cursor c1 is select col1, col2 from table1; 
    l_col1 table1.col1%type; 
    l_col2 table1.col2%type; 
    l_col3 table2.col3%type; 
begin 
    open c1; 
    loop 
    fetch c1 into l_col1, l_col2; 
    exit when c1%notfound; 


    select col3 
     into l_col3 
     from table2 t 
    where t.col1 = l_col1 --Assuming there is exactly one row in table2 
     and t.col2 = l_col2; --satisfying these conditions 

    end loop; 
    close c1; 
end; 

, 그럼 그것도 간단 :이 예에서

declare 
    l_col3 table2.col3%type; 
begin 
    for i in (select col1, col2 from table1) 
    loop 

    select col3 
     into l_col3 
     from table2 t 
    where t.col1 = i.col1 --Assuming there is exactly one row in table2 
     and t.col2 = i.col2; --satisfying these conditions  

    end loop; 
end; 

, 그것은

begin 
    for i in (select t1.col1 
       , t1.col2 
       , (select t2.col3 
         from table2 t2 
        where t2.col1 = t1.col1 --Assuming there is atmost one such 
         and t2.col2 = t1.col2 --row in table2 
        ) col3 
       from table1 t1) 
    loop 
    ...   
    end loop; 
end; 
관련 문제