CREATE OR REPLACE
package pkg_test as
/* 定义ref cursor类型
  不加return类型,为弱类型,允许动态sql查询,
  否则为强类型,无法使用动态sql查询;
*/
 type myrctype is ref cursor; --函数申明
 function get(intID number) return myrctype;
end pkg_test;
/CREATE OR REPLACE
package body pkg_test as
--函数体
  function get(intID number) return myrctype is
    rc myrctype;  --定义ref cursor变量
    sqlstr varchar2(500);
  begin
    if intID=0 then
       --静态测试,直接用select语句直接返回结果
       open rc for select id,name,sex,address,postcode,birthday from student;
    else
       --动态sql赋值,用:w_id来申明该变量从外部获得
       sqlstr := 'select id,name,sex,address,postcode,birthday from student where id=:w_id';
       --动态测试,用sqlstr字符串返回结果,用using关键词传递参数
       open rc for sqlstr using intid;
    end if;    return rc;
  end get;end pkg_test;
/

解决方案 »

  1.   

    谢谢!
    若用record,table怎么写?
      

  2.   

    若用record,table怎么写?指的是什么?是要返回record,table,还是其他?
      

  3.   

    接 nicholaz(九思·逢尤) 兄作测试
    3、用pl/sql块进行测试:
    declare
      w_rc       pkg_test.myrctype; --定义ref cursor型变量
     
      --定义临时变量,用于显示结果
      w_id       student.id%type;
      w_name     student.name%type;
      w_sex      student.sex%type;
      w_address  student.address%type;
      w_postcode student.postcode%type;
      w_birthday student.birthday%type;
     
    begin
      --调用函数,获得记录集
      w_rc := pkg_test.get(1);
     
      --fetch结果并显示
     loop
     fetch w_rc into w_id,w_name,w_sex,w_address,w_postcode,w_birthday;
     exit when w_rc%notfound;
     dbms_output.put_line(w_name);
     end loop;
    end;
     
    4、测试结果:
    通过。
      

  4.   

    LGQDUCKY(飘) 要返回record,tableThank you!
      

  5.   

    package spec   type userrecord is record(user_no varchar2(8),user_name varchar2(32));
       function RvUsers return userrecord;package body   function RvUsers return userrecord
       is 
           v_cursor1 cursortype;
           out_record userrecord;
       begin
           open v_cursor1 for select user_no,user_name from users;
           loop
                fetch v_cursor1 into out_record.user_no,out_record.user_name;
                exit when v_cursor1%notfound;        
           end loop;
           close v_cursor1;
           return out_record;
       end;
      

  6.   

    我有个办法
    就是用临时表
    把record,table的数据放在一个临时表中
    你再在程序中读表,不就可以了吗?