1. 自定义 ref cursor 和 sys_refcursor; 
2. sys_refcursor 做为参数传递结果集; 
3. ref cursor 做为参数传递结果集;

1. 自定义 ref cursor 和 sys_refcursor:

  declare
  type df_ref is ref cursor; --定义 ref cursor
  rf df_ref; --声明 rf 是df_ref
  ename varchar2(30);
 begin
  open rf for 'select ename from emp';
  loop
   fetch rf into ename;
   dbms_output.put_line(ename);
  exit when rf%notfound;
 end loop;
 close rf;
end;
/

sys_refcursor 不需要声明可以直接使用:

 declare
 reft sys_refcursor;
  begin
   open reft for 'select * from emp';
  close  reft;
 end;

sqlplus 中可以使用refcursor:

OPS$SYWU@sydb%11GR2>variable r refcursor;
OPS$SYWU@sydb%11GR2>exec open :r for 'select * from emp';

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.00
OPS$SYWU@sydb%11GR2>print :r;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

14 rows selected.

2. sys_refcursor 做为参数传递结果集:

 create or replace procedure pro_getEmp(ref_rs out sys_refcursor)
 is
  begin
  open ref_rs for 'select ename,empno from emp';
  ---不能在这里关闭
 end;
/

调用结果集:

declare
refc sys_refcursor;
ename varchar2(30);
empno number;
begin
 pro_getEmp(ref_rs=>refc);
 loop
    fetch refc into ename,empno;
  dbms_output.put_line(ename||''||empno);
 exit when refc%notfound;
 end loop;
end;
/

3. ref cursor 做为参数传递结果集: 
   在包头定义 ref cursor:

create or replace package pk_cur
  as
  type df_cursor is ref cursor;

  function fun_emp return df_cursor;
 end;
/

 create or replace package body pk_cur
  is

   function fun_emp return df_cursor
     is
	fn_cursor df_cursor;
     begin
       open fn_cursor for 'select * from emp';
     return fn_cursor;
   end;
end;
/

OPS$SYWU@sydb%11GR2>  select pk_cur.fun_emp from dual;

FUN_EMP
--------------------
CURSOR STATEMENT : 1

CURSOR STATEMENT : 1

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

14 rows selected. 

来源:https://www.cnblogs.com/lanston/p/3993936.html

Oracle ref cursor和sys_refcursor的更多相关文章

  1. oracle sys_refcursor用法和ref cursor区别

    --创建过程,参数为sys_refcursor,为out型 create or replace procedure aabbsys_refcursor(o out sys_refcursor) is ...

  2. oracle 中 cursor 与refcursor及sys_refcursor的区别 (转载)

    http://blog.csdn.net/gyflyx/article/details/6889028 引用一.显式cursor 显式是相对与隐式cursor而言的,就是有一个明确的声明的cursor ...

  3. oracle中REF Cursor用法

    from:http://www.111cn.net/database/Oracle/42873.htm 1,什么是 REF游标 ? 动态关联结果集的临时对象.即在运行的时候动态决定执行查询. 2,RE ...

  4. ORACLE中RECORD、VARRAY、TABLE、IS REF CURSOR 的使用及实例详解

    ORACLE中RECORD.VARRAY.TAB.IS REF CURSOR LE的使用及实例详解 create or replaceprocedure PRO_RECORD_ROW_TAB_EXAM ...

  5. oracle 存储过程及REF CURSOR的使用

    基本使用方法及示例 1.基本结构: CREATE OR REPLACE PROCEDURE 存储过程名字 (参数1 IN NUMBER,参数2 IN NUMBER) AS 变量1 INTEGER := ...

  6. oracle ref游标

    Oracle 系列:REF Cursor 在上文  Oracle 系列:Cursor  (参见:http://blog.csdn.net/qfs_v/archive/2008/05/06/240479 ...

  7. Entity Framework 5.0.0 Function Import 以及 ODP. NET Implicit REF CURSOR Binding使用简介

    源代码 概要: 1,说明如何使用Entity Framework中的function import功能. 2,说明如何使用ODP.NET的隐式REF CURSOR绑定(implicit REF CUR ...

  8. Converting REF CURSOR to PIPE for Performance in PHP OCI8 and PDO_OCI

    原文地址:https://blogs.oracle.com/opal/entry/converting_ref_cursor_to_pipe REF CURSORs are common in Ora ...

  9. oracle的cursor

    oracle的cursor 转自:http://www.cnblogs.com/shengtianlong/archive/2010/12/31/1922767.html 1,什么是游标? ①从表中检 ...

随机推荐

  1. jQuery学习笔记(jquery.validate插件)

    jquery.validate官网地址:http://jqueryvalidation.org/ 1. 导入JavaScript库 <script src="../js/jquery. ...

  2. [转载]Cortana 设计指导方针

    来源:@微软中国MSDN,源地址:http://weibo.com/p/1001603898586285925224 使用语音命令,延伸 Cortana 与您的应用程序所提供的功能.启动应用程序,启动 ...

  3. 【Android开发】构建Android源码编译环境

    原文:http://android.eoe.cn/topic/android_sdk 构建Android源码编译环境 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  4. git add 不必要的文件 如何撤回

    [root@666 IT-DOC]# git status -s ?? a.txt [root@666 IT-DOC]# git add a.txt [root@666 IT-DOC]# git st ...

  5. Stochastic Gradient Descent收敛判断及收敛速度的控制

    要判断Stochastic Gradient Descent是否收敛,可以像Batch Gradient Descent一样打印出iteration的次数和Cost的函数关系图,然后判断曲线是否呈现下 ...

  6. 笔记 Hadoop

    今天有缘看到董西成写的<Hadoop技术内幕:深入解析MapReduce架构设计与实现原理>,翻了翻觉得是很有趣的而且把hadoop讲得很清晰书,就花了一下午的时间大致拜读了一下(仅浏览了 ...

  7. 关于 Nginx upstream keepalive 的说明

    模块是 HttpUpstreamModule,配置的一个例子: [shell]upstream http_backend {    server 127.0.0.1:8080; keepalive 1 ...

  8. android category

    本章节翻译自<Beginning-Android-4-Application-Development>,如有翻译不当的地方,敬请指出. 原书购买地址http://www.amazon.co ...

  9. Asp.Net MVC App_Code无法识别

    Asp.Net MVC需要写公共类的时候 右击添加 App_Code 文件夹,新建类—>右击类—>属性,生成操作 —>选择 —>编译 Asp.Net MVC项目本身是个应用程序 ...

  10. python 获取环境变量

    python 获取环境变量 参考 https://docs.python.org/2/library/os.html 使用os.environ获取环境变量字典 import os env_dist = ...