代码没有错误。
提醒以下:
    1) 没有记录时,仍执行 fetch XXX into XXX,然后 执行 exit when daydata%notfound 
    2)如果有业务处理,放在 “exit when daydata%notfound;“ 的后面

解决方案 »

  1.   

    十分感谢,希望你能继续帮助我
       我的业务处理放在“exit when daydata%notfound;“ 的前面了,因为我业务处理中要对i赋值。使得游标查到的每一行都按照一定的条件进行处理,处理过程中始终要用data1,data2,data3,即是不断变化的。
       请指教!
      

  2.   

    从上面的代码看,没有问题。你只要考虑一下,(执行到)没有记录时,仍执行 fetch XXX into XXX,然后 执行 exit when daydata%notfound ,再退出loop.
      

  3.   

    在上面的这个语句:
    fetch daydata into data3;
      之后:
      if data3.gatherid!=data2.gatherid then
         data1:=data3;
             i:=1;--让它又进入循环
         (do..)
      else 
             i:=0;--又重新进入循环
         (do..)
      end if;
     exit daydata%notfound;
     end loop;
     close daydata;
    以上这样呢?请指教。
      

  4.   

    有PLSQL Developer 吗,跟踪执行。或者添加 dbms_output.put_line
      

  5.   

    谢谢,再请教一下:
    表1:                        表2:                     表3:
    gatherid   lineid         data    gatherid   time      lineid  sum(data) time
       1         1              3         1     20031205                     20031205
       2         1              2         2     20031205 
       3         1              4         3     20031204
       4         2              5         6     20031206
       5         2              .         .     20031205
       6         1              .         .        .
       .         .
       .         .
    很多个gatherid属于一个lineid,
    我如何将20031205日的所有lineid对应的表1的所有gatherid对应表2的data求出总和,保存在表3的sum(data)?
      

  6.   

    表1:                        表2:                     表3:
    gatherid   lineid       data    gatherid   time       lineid  sum(data) time
       1         1           3         1     20031205                       20031205
       2         1           2         2     20031205 
       3         1           4         3     20031204
       4         2           5         6     20031206
       5         2           .         .     20031205
       6         1           .         .        .
       .         .
       .         .
      

  7.   

    表1:                  表2:                     表3:
    gatherid lineid   data    gatherid   time       lineid  sum(data) time
       1       1        3        1     20031205                      20031205
       2       1        2        2     20031205 
       3       1        4        3     20031204
       4       2        5        6     20031206
       5       2        .        .     20031205
       6       1        .        .        .
       .       .
       .       .
      

  8.   

    下面只是个思路:
    ...
    cursor c1 is select sum(fld) from 表1 group by lineidbegin
       
      open c1;
      
      loop
         insert into 表3 (
      end loop
      

  9.   

    谢谢,我再想是否能用一条语句写出来,
    感谢你的热情,请留下QQ,以后便于交流,THANKS·
      

  10.   

    Oracle 8i是不可能 用一条SQL 实现多条记录 INSERT ,但可以Update.
      

  11.   

    if i=0 then 
       fetch daydata into data1;
       i:=1;
      elsif i=1 then
       fetch daydata into data2;
       if data2.gatherid=data1.gatherid then
         i:=2;
       else
         data1:=data2;
         i:=1;
       end if;
      elsif i=2 then
       fetch daydata into data3;
          ...
      end if;
      exit when daydata%notfound;以上这个循环fetch三次值,哪么有可能当中游标已结尾,exit when daydata%notfound;已失效了.再者每次fetch都是一条记录.
      

  12.   

    insert into tab3 
    select lineid,  sum(data), '20031205'
      from tab1 a,tab2 b
     where a.gatherid = b.gatherid
       and b.time = '20031205'
     group by lineid;
      

  13.   

    均以解决,谢谢大家,现在结帖,之前问一个小问题:
    PLSQL中有没有这样一个函数(假设为X函数)
       X(a,b,1,0)
      如果a大于b则返回1,小于则返回0
    ????
      

  14.   

    先判断“exit when daydata%notfound;“ 后执行,否则会出现为空的情况。
      

  15.   

    不会,大家仔细想想,循环中每条FETCH总会到exit when daydata%notfound处,而且每次只是一条fetch,和教科书上的例子不同。
    PLSQL中有没有这样一个函数(假设为X函数)
       X(a,b,1,0)
      如果a大于b则返回1,小于则返回0