我在存储过程中建立一个游标,其中有一条语句是从表中提取一条符合要求的字段(date型的),但是我不能保证每个用户都有这么一条记录,譬如新建用户就没有上月的纪录,当存储过程执行到这种纪录时,就找不到这个字段,就会报错,请问我如何处理,才能让存储过程不报错?

解决方案 »

  1.   

    先判断while 游标%FOUND loop
      

  2.   

    使用异常处理好了
    begin
    ....
    select date into aaa from table1;
    EXCEPTION  then
    end;  
      

  3.   

    不是在游标中提不出数来,是在过程中!!下面是部分代码:有省略
    cursor init  is select userid from  input;
    open init;
    loop
    fetch init into lluserid;
    exit when init%notfound;
    select tb1.copydate
    into oldcopydate1
    from (select row_number()over(order by year desc,usemonth desc) rn,copydate,real from history where userid=lluserid ) tb1
    where rn=1;
    是这个选择出来的东西没找到(tb1.copydate)
    如何处理?
      

  4.   

    cursor init  is select userid from  input;
    open init;
    loop
    fetch init into lluserid;
    exit when init%notfound;begin
        select tb1.copydate
        into oldcopydate1
        from (select row_number()over(order by year desc,usemonth desc)  rn,copydate,real   from history where userid=lluserid ) tb1
    where rn=1;exception
       when no_data_found then
           null;
       when others then
          db_output.put_line(sql_code);
    end;end loop