PL / SQL游标问题

时间:2012-08-17 10:46:08

标签: oracle plsql

在下面的过程中使用游标cur1获取错误。我可以像这样创建第二个游标吗?如果没有,请提出建议。

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;

1 个答案:

答案 0 :(得分: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;