需要做这样的一件事情:在ORACLE数据库定义一个包和包体,要求返回值Type Resultset Is Ref Cursor;。在包体中先定义一个游标。 然后循环这个游标,做更新处理(更新某列数据)。 然后把把游标作为返回值返回。谢谢!请高手们给个示范的例子 

解决方案 »

  1.   

    create package my as
          type curtype is ref cursor return test%ROWTYPE;
       procedure open_cv( cur_cv in out curtype);
    end;create or replace package body my as
      procedure open_cv(cur_cv in out curtype)
      as
        v_f1 number(10);  
        cursor cur_cv1 is select f1 from test where f1>1 for update;
      begin
        open cur_cv1;
        fetch cur_cv1 into v_f1;
        while(cur_cv1%found)
        loop
            update t1 set f1 = f1 + 1 where current of cur_cv1;
            fetch cur_cv1 into v_f1;
        end loop;        
        close cur_cv1;
        
        open cur_cv for select f1 from test where f1>1;
       end;
       end;
       /需要说明的是,游标的方向是单向的,每次fetch就往前进了一步,是不能往后退的,所以,这里先定义一个游标完成你需要的更新操作。更新操作做完以后,再打开一个游标返回。
      

  2.   

    谢谢runbaobao88。
    有没有不用查询两次的方法呢?
    比如不用fetch将一个游标内容复制给另一个游标呢?
      

  3.   

    前面这个fetch是因为你要遍历游标做更新操作所以搞了两个游标。如果你不需要用游标遍历,那么只需要一个游标就可以了。另外游标没有复制的,只有定义同样查询的游标。