0

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;
4

2 回答 2

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;

希望能帮助到你...

编辑:

我稍微编辑了您的代码以使其更可行,只需将您注释掉的插入或更新语句替换为正确的代码,您就可以开始使用了。

编辑2:

如果您真的想在过程中间声明游标,请使用匿名 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;
于 2012-08-17T11:53:18.877 回答
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;
于 2012-08-17T12:59:25.557 回答