我现在有游标c_cursorRoom ,查询出的结果,当做游标c_cursor 的参数进行查询,然后再执行新增动作。我直接用游标2的sql语句select id,c_m_id from s_table1 where c_m_id<>'0000' and c_template_id='123';查询出数据库中结果才20条,但放到游标中执行,就上百条。不知道那里搞错了,麻烦给指点下。谢谢declare
       cursor c_cursorRoom 
        is select id,c_template_id from s_table1 where c_m_id='0000';
       cursor c_cursor (temPId varchar2)
       is select id,c_m_id from s_table1 where c_m_id<>'0000' and c_template_id<=temPId ;
       v_destTblId s_table1.id%type; 
       v_mmroomId s_table1.id%type;
       
       v_PtableId s_table1.id%type;
       v_PtempId s_table1.c_template_id%type; 
       vCount number(10):=0;
       vCount_out number(10):=0;
begin       open c_cursorRoom;
          fetch c_cursorRoom into v_PtableId,v_PtempId;
          while c_cursorRoom%Found loop
                vCount_out :=vCount_out+1;
              open c_cursor(temPId =>v_PtempId);
               fetch c_cursor into v_destTblId,v_mmroomId;
               while c_cursor%Found loop
                    
                     insert into s_table2 values(sys_guid(),'0000',v_PtempId,v_PtableId,v_mmroomId,v_destTblId);
                     vCount := vCount+1;
                       fetch c_cursor into v_destTblId,v_mmroomId;
               end loop;
             close c_cursor;
              fetch c_cursorRoom into v_PtableId,v_PtempId;
            
          end loop;
          dbms_output.put_line('--vCount:'||to_char(vCount)||':'||vCount_out);
       close c_cursorRoom;
      
end;

解决方案 »

  1.   

    select id,c_m_id from s_table1 where c_m_id<>'0000' and c_template_id='123';你游标中用的是<=select id,c_m_id from s_table1 where c_m_id<>'0000' and c_template_id<=temPId ;
      

  2.   

    c_template_id<=temPId条件范围不一样,结果当然不一样,你这个大过你上面那个的范围的
      

  3.   

    哦。谢谢。我以为<= 时赋值呢。。为什么在循环前, fetch c_cursorRoom into v_PtableId,v_PtempId; ,
    循环中间也有: fetch c_cursorRoom into v_PtableId,v_PtempId;  。还有其他的循环游标方式吗
      

  4.   

    楼主的代码写的很糟,逻辑更是一塌糊涂。。v_destTblId 这个变量的用处在哪里?一个sql搞不定,非得去循环?为什么同一个游标要fetch2次?
      

  5.   

    你好,不知道一个sql 怎么实现。。
    v_destTblId 这个变量在执行插入操作时用到了。
    insert into s_table2 values(sys_guid(),'0000',v_PtempId,v_PtableId,v_mmroomId,v_destTblId); 
    还有就是我也不理解为什么 要fetch2次。我看一个教程上就时这么写的。
      

  6.   

    里面那个用insert into ... select ...就可以了吧
      

  7.   

    同意5L,两个游标的sql完全可以关联查询出最终结果,楼主先试试吧。数据量少就insert into select一句解决。
    量太多也就一个游标,分批提交。