create or replace procedure a is
  v_routeid       ia_ia_bill_head.src_bill_id%TYPE;  cursor term_type is     SELECT  l.src_bill_id
      FROM  ia_ia_bill_line l
     where  l.id = 758;begin
  Open term_type;
  loop
    fetch term_type
      into v_routeid; --从游标取值    dbms_output.put_line('1');     
            if term_type%notfound then
              exit;
            end if;  end loop;
  close term_type;
end a;
上面是一个最基本的存储过程。。我奇怪的是,那个游标的select明显只有1条数据,为什么会打印2次?也就是说fetch游标了2次,这肯定不对的,是我loop的时候哪里出问题了?
求教,,

解决方案 »

  1.   

    注:当我要实现业务循环的时候,那个游标就不止一条记录了。所以不要建议我,只有一条记录,直接用selelct  into  语句不用游标。
      

  2.   

    exit 放错位置了
        create or replace procedure a is
      v_routeid       ia_ia_bill_head.src_bill_id%TYPE;  cursor term_type is     SELECT  l.src_bill_id
          FROM  ia_ia_bill_line l
         where  l.id = 758;begin
      Open term_type;
      loop
        fetch term_type
          into v_routeid; --从游标取值   
          EXIT WHEN term_type%NOTFOUND;    dbms_output.put_line('1');
      end loop;
      close term_type;
    end a;
      

  3.   

    原因可能是这样的。oracle取到最后一条的时候,是没法判断是不是最后一条,他需要再循环一次,比较两条的rowid什么的,确认是两次取的是同一条数据,才能证实已经是最后一条。所以你上面的过程会打印两次
      

  4.   

    begin
    open term_type;
    loop
    fetch term_type into v_routeid;--从游标取值
    exit when term_type%notfound;--判断v_routeid里面是否有值,无值时跳出;
    dbms_output.put_line('1');
    end loop;
    end--显示1;
    你写的顺序是先显示1,下面判断,明显是有值,又执行了一次,显示1,无值,跳出;
      

  5.   

    理解了。。loop的时候应该if term_type%notfound then
                  exit;
                end if;
    放前面的。。