2012-08-17 98 views
0

在下面的過程中使用光標cur1獲取錯誤。我可以創建這樣的第二個遊標嗎?如果沒有,請提供建議。PL/SQL光標問題

procedure my_procedure as 

cursor cur is  
select objects,un_fact,min__dt min, max_dt max 
    from table1 obj 
where obj.o = 'SOMECONDITION'; 

-- below pat_key is a table type of varchar2 
patkey_id pat_key;  
var_attr number;  

begin  
    for x in cur loop 
     begin 
     patkey_id := null;  
     var_attr:=null; 

     select t2.pat_key bulk collect into patkey_id 
     from table2 t2, table1 o 
     where t2.obj_id = x.objects 
     and t2.pat_key = o.key; 

     if length(patkey_id) = 0 then  
     select t2.pat_key bulk collect into patkey_id 
      from table2 t2,table1 o 
      where some other conditions; 
     end if; 

     for i in patkey_id.first..patkey_id.last loop 
     if var_attr = null then  
      var_attr := patkey_id(0); 
     else 
      var_attr := var_attr||','||patkey_id(i); 
     end if; 
     end loop; 

     -- another cursor  
     cursor cur1 is  
     SELECT key_1,key_2 
     FROM table3 t3 
     WHERE t3.pat_key_id = x.min 
      and t3.some1= x.max 
      and t3.some2= var_attr; 

    begin 

     for y in cur1 loop 
     begin 
      if t3.key_1 = 'something1' then  
      --update or insert 
      elsif t3.key_1='something2' then 
      --update or insert; 
      end if; 
    end loop; 

    end loop; 

end my_procedure; 
+2

你有什麼問題?我們無法提供詳細的錯誤說明。 – 2012-08-17 10:52:00

+0

我試圖讓你的代碼可讀......它顯示了大量的錯誤。也就是說,你不會在這裏終止任何PL/SQL塊。在回答你的問題時,你不能。如果需要,可以在頂層聲明它,或者在中間的聲明節中聲明它。 – Ben 2012-08-17 10:54:17

+0

錯誤:錯誤:PLS-00103:遇到下列其中一項時遇到符號「CUR1」: :=。 (@%; – user1606655 2012-08-17 11:05:47

回答

3

在變量聲明區域中聲明第二個遊標,然後如圖所示引用它。 我已經使用遊標參數來幫助您傳遞給第二個遊標的變量。

您的代碼是不完整的,但這應該幫助你:

procedure my_procedure 
as 
    cursor cur is 
     select objects,un_fact,min__dt min, max_dt max 
     from table1 obj  
     where obj.o = 'SOMECONDITION'; -- below pat_key is a table type of varchar2 

    cursor cur1 is (
     cp_some2_param IN NUMBER 
    ) 
     SELECT key_1,key_2 
     FROM table3 t3 
     WHERE t3.pat_key_id = x.min 
     and t3.some1= x.max 
     and t3.some2= cp_some2_param; 

    patkey_id pat_key;  
    var_attr number;  
begin   
    for x in cur 
    loop    
     patkey_id := null;   
     var_attr := null; 

     select t2.pat_key 
     bulk collect into patkey_id   
     from table2 t2, table1 o   
     where t2.obj_id = x.objects   
     and t2.pat_key = o.key;   

     if length(patkey_id) = 0 
     then    
     select t2.pat_key 
      bulk collect into patkey_id    
      from table2 t2,table1 o   
      where some other conditions;   
     end if;   

     for i in patkey_id.first..patkey_id.last 
     loop   
     if var_attr = null 
     then     
      var_attr := patkey_id(0);   
     else 
      var_attr := var_attr||','||patkey_id(i);   
     end if;   
     end loop;  

     for y in cur1(var_attr) 
     loop 
     if t3.key_1 = 'something1' 
     then 
      --update or insert 
     elsif t3.key_1='something2' 
     then 
      --update or insert; 
     end if; 
     end loop; 
    end loop; 
end my_procedure; 

希望它可以幫助...

編輯:

我稍微修改代碼,使其更可行的,只需用適當的代碼替換你的註釋掉的插入或更新語句,你應該很好。

EDIT2:

如果你真的想聲明的過程中間的光標,然後使用匿名PL/SQL塊,如下圖所示:

procedure my_procedure 
as 
    cursor cur is 
     select objects,un_fact,min__dt min, max_dt max 
     from table1 obj  
     where obj.o = 'SOMECONDITION'; -- below pat_key is a table type of varchar2 

    patkey_id pat_key;  
    var_attr number;  
begin   
    for x in cur 
    loop    
     patkey_id := null;   
     var_attr := null; 

     select t2.pat_key 
     bulk collect into patkey_id   
     from table2 t2, table1 o   
     where t2.obj_id = x.objects   
     and t2.pat_key = o.key;   

     if length(patkey_id) = 0 
     then    
     select t2.pat_key 
      bulk collect into patkey_id    
      from table2 t2,table1 o   
      where some other conditions;   
     end if;   

     for i in patkey_id.first..patkey_id.last 
     loop   
     if var_attr = null 
     then     
      var_attr := patkey_id(0);   
     else 
      var_attr := var_attr||','||patkey_id(i);   
     end if;   
     end loop;  

     DECLARE 
     cursor cur1 is 
      SELECT key_1,key_2 
       FROM table3 t3 
      WHERE t3.pat_key_id = x.min 
       and t3.some1= x.max 
       and t3.some2= var_attr; 
     BEGIN 
     for y in cur1 
     loop 
      if t3.key_1 = 'something1' 
      then 
       --update or insert 
      elsif t3.key_1='something2' 
      then 
       --update or insert; 
      end if; 
     end loop; 
     END; 
    end loop; 
end my_procedure; 
+0

@tbone還增加了一個很好的例子隱式遊標聲明在代碼本身中,我的例子都是顯式遊標。 – Ollie 2012-08-17 13:39:56

2

只需使用第二光標像這樣:

DECLARE 
    -- cursor 1 
    cursor c1 is 
    select col1 from tab1; 

BEGIN 
    for rec in c1 
    loop 
    -- use tab1 data here 

    -- cursor 2 
    for rec2 in (select col2 from tab2 where col1=rec.col1) 
    loop 
     -- use tab2 data here 

    end loop; 

    end loop; 

END;