Ref Cursor / sys_refcursor

REF CURSOR

OPEN-FOR、FETCH和CLOSE语句被用于处理多行查询或者游标。这个概念非常类似于静态的游标处理。对于静态游标,首先把游标变量与查询建立关联。接下来,打开游标变量,以便于指向结果集的第一行数据。然后,从结果集中每次检索一行数据。最后,当处理完所有数据行是,关闭游标


对于动态SQL,OPEN-FOR语句有个可选的USING子句,使用它可以在运行时间向绑定变量参数传值。OPEN-FOR语句的通用语法如下所示(方括号中的保留字和短语都是可选的);

 OPEN cursor_variable FOR dynamic_SQL_string

     [USING bind_argument1, bind_argument2, ...]


cursor_variable是一种若类型的REF CURSOR变量,并且dynamic_sql_string是包含多行查询的字符串

   
   
  1. DECLARE
  2. type student_cur_type is REF CURSOR;
  3. student_cur student_cur_type;
  4. type student_type is table of student%rowtype index by pls_integer;
  5. student_tab student_type;
  6. v_student_id student.student_id%type := &student_id;
  7. v_count integer := 0;
  8. BEGIN
  9. open student_cur for
  10. 'select first_name,last_name from student where student_id < :1'
  11. using v_student_id;
  12. LOOP
  13. FETCH student_cur into student_tab(v_count).first_name,student_tab(v_count).last_name;
  14. exit when student_cur%notfound;
  15. v_count := v_count + 1;
  16. end loop;
  17. for i in 1..v_count loop
  18. DBMS_OUTPUT.PUT_LINE('First name :'||student_tab(i).first_name||' '||'Last name :'||student_tab(i).last_name);
  19. end loop;
  20. close student_cur;
  21. EXCEPTION
  22. when others then
  23. if student_cur%isopen then
  24. close student_cur;
  25. end if;
  26. DBMS_OUTPUT.PUT_LINE('ERROR: '||substr(sqlerrm,1,200));
  27. end;
  28. /
    
    
  1. sys_refcursororacle9i以后系统定义的一个refcursor,主要用在过程中返回结果集。
  2. create or replace procedure getEmpByDept(in_deptNo in emp.deptno%type,
  3. out_curEmp out SYS_REFCURSOR) as
  4. begin
  5. open out_curEmp for
  6. SELECT * FROM emp WHERE deptno = in_deptNo ;
  7. EXCEPTION
  8. WHEN OTHERS THEN
  9. RAISE_APPLICATION_ERROR(-20101,
  10. 'Error in getEmpByDept' || SQLCODE );
  11. end getEmpByDept;
  12. /
  13. scott@ORCL>var rset refcursor;
  14. scott@ORCL>exec getEmpByDept(10,:rset);
  15. PL/SQL procedure successfully completed.
  16. scott@ORCL>print rset
  17.     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
  18. ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
  19.      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
  20.      7839 KING       PRESIDENT            17-NOV-81       5000                    10
  21.      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10
  22. CREATE OR REPLACE FUNCTION Get_emp
  23. RETURN SYS_REFCURSOR
  24. IS
  25.  return_cursor SYS_REFCURSOR;
  26. BEGIN
  27.  OPEN return_cursor FOR 'SELECT * FROM emp';
  28.  RETURN return_cursor;
  29. END;
  30. select Get_emp() from dual;
  31. GET_EMP()
  32. --------------------
  33. CURSOR STATEMENT : 1
  34. CURSOR STATEMENT : 1
  35.     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
  36. ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
  37.      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
  38.      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
  39.      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
  40.      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
  41.      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
  42.      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
  43.      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
  44.      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
  45.      7839 KING       PRESIDENT            17-NOV-81       5000                    10
  46.      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
  47.      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
  48.      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
  49.      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
  50.      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10
  51. 14 rows selected.
  52. -- 存储过程调用, 来获取结果.
  53. DECLARE
  54.  -- 调用函数的返回值.
  55.  testCursor SYS_REFCURSOR;
  56.  -- 存储单行数据.
  57.  testRec  emp%ROWTYPE;
  58. BEGIN
  59.  -- 调用返回结果集的函数.
  60.  testCursor := Get_emp();
  61.  -- 循环,遍历结果.
  62.  LOOP
  63.    -- 游标向前.
  64.    FETCH testCursor INTO testRec;
  65.    -- 无数据的情况下,退出循环.
  66. EXIT WHEN testCursor%NOTFOUND;
  67.    -- 输出调试信息.
  68.    dbms_output.put_line( TO_CHAR(testRec.empno) || ' ' || testRec.ename);
  69.  END LOOP;
  70. END;
  71. /
  72. 7369 SMITH
  73. 7499 ALLEN
  74. 7521 WARD
  75. 7566 JONES
  76. 7654 MARTIN
  77. 7698 BLAKE
  78. 7782 CLARK
  79. 7788 SCOTT
  80. 7839 KING
  81. 7844 TURNER
  82. 7876 ADAMS
  83. 7900 JAMES
  84. 7902 FORD
  85. 7934 MILLER




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值