给你个例子:
/*包头:定义参考游标,定义函数,返回*/
create or replace package dinya_pkg_test
as
  type myCursor is ref cursor;
  function get(p_id number) return myCursor;
end dinya_pkg_test;
--======================================================
create or replace package body dinya_pkg_test 
as
  --*****************************************************************************
  --输入ID 返回记录集的函数
  function get(p_id number) return myCursor is
     rc myCursor;
     strsql varchar2(200);
  begin
     if p_id=0 then 
        open rc for select a.user_name from fnd_user a ;  
     else
        strsql:='select a.user_name from fnd_user a where a.user_id=:p_id';
        open rc for strsql;
     end if;
     return rc;  
     end get;
end dinya_pkg_test;*****************************************************
--调用:
set serverout on 
declare 
  w_rc dinya_pkg_test.myCursor;
  w_name varchar2(100);
begin
  w_rc:=dinya_pkg_test.get(0);
  loop
  fetch w_rc into w_name;
        exit when w_rc%notfound;
  dbms_output.put_line(w_name);
  end loop;
end;
/

解决方案 »

  1.   

    create or replace procedure pro 
    is 
    type ty is ref cursor;
    vstr varchar2(100);
    vemp emp%rowtype;
    ta ty;
    begin
    vstr:='select * from emp';open ta for vstr;
    loop
    fetch ta into vemp;
    exit when ta%notfound;
    ---操作
    end loop;
    close ta;
    end pro;
      

  2.   

    CREATE OR REPLACE PACKAGE pkg_test
    AS
       TYPE myrctype IS REF CURSOR;   PROCEDURE get (p_id NUMBER, p_rc OUT myrctype);
    END pkg_test;
    /CREATE OR REPLACE PACKAGE BODY pkg_test
    AS
       PROCEDURE get (p_id NUMBER, p_rc OUT myrctype)
       IS
          sqlstr   VARCHAR2 (500);
       BEGIN
          IF p_id = 0 THEN
             OPEN p_rc FOR
                SELECT ID, NAME, sex, address, postcode, birthday
                  FROM student;
          ELSE
             sqlstr :=
                'select id,name,sex,address,postcode,birthday
               from student where id=:w_id';
             OPEN p_rc FOR sqlstr USING p_id;
          END IF;
       END get;
    END pkg_test;
    /
      

  3.   

    在多问一句哈,下面句中是SQL Server存储过程中的一部分,:@tempIndexId,,@tempIndexName等是从游标中刚fetch出来临时保存数据的变量,如何移植到Oracle中呢??/长期困扰。
    IF @resultNum = 1
    BEGIN
    select @tempIndexType = (select distinct dataType from dbo.CAF_PM_IndexDef_Table where indexId = @tempIndexId)
    SELECT @tempIndexId,@tempIndexName,@tempIndexType,@tempIndexMaxThreshold,@tempIndexMinThreshold,@tempIsAlarm
    CLOSE  cusor_records 
    DEALLOCATE  cusor_records 
    return
    END