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

1. 自定义 ref cursor 和 sys_refcursor:

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

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

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

sqlplus 中可以使用refcursor:

  1. OPS$SYWU@sydb%11GR2>variable r refcursor;
  1. OPS$SYWU@sydb%11GR2>exec open :r for 'select * from emp';
  1.  
  1. PL/SQL procedure successfully completed.
  1.  
  1. Elapsed: 00:00:00.00
  1. OPS$SYWU@sydb%11GR2>print :r;
  1.  
  1. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
  1. ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
  1. 7369 SMITH CLERK 7902 17-DEC-80 800 20
  1. 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
  1. 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
  1. 7566 JONES MANAGER 7839 02-APR-81 2975 20
  1. 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
  1. 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
  1. 7782 CLARK MANAGER 7839 09-JUN-81 2450 10
  1. 7788 SCOTT ANALYST 7566 19-APR-87 3000 20
  1. 7839 KING PRESIDENT 17-NOV-81 5000 10
  1. 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
  1. 7876 ADAMS CLERK 7788 23-MAY-87 1100 20
  1.  
  1. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
  1. ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
  1. 7900 JAMES CLERK 7698 03-DEC-81 950 30
  1. 7902 FORD ANALYST 7566 03-DEC-81 3000 20
  1. 7934 MILLER CLERK 7782 23-JAN-82 1300 10
  1.  
  1. 14 rows selected.

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

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

调用结果集:

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

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

  1. create or replace package pk_cur
  1. as
  1. type df_cursor is ref cursor;
  1.  
  1. function fun_emp return df_cursor;
  1. end;
  1. /
  1.  
  1. create or replace package body pk_cur
  1. is
  1.  
  1. function fun_emp return df_cursor
  1. is
  1. fn_cursor df_cursor;
  1. begin
  1. open fn_cursor for 'select * from emp';
  1. return fn_cursor;
  1. end;
  1. end;
  1. /
  1.  
  1. OPS$SYWU@sydb%11GR2> select pk_cur.fun_emp from dual;
  1.  
  1. FUN_EMP
  1. --------------------
  1. CURSOR STATEMENT : 1
  1.  
  1. CURSOR STATEMENT : 1
  1.  
  1. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
  1. ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
  1. 7369 SMITH CLERK 7902 17-DEC-80 800 20
  1. 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
  1. 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
  1. 7566 JONES MANAGER 7839 02-APR-81 2975 20
  1. 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
  1. 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
  1. 7782 CLARK MANAGER 7839 09-JUN-81 2450 10
  1. 7788 SCOTT ANALYST 7566 19-APR-87 3000 20
  1. 7839 KING PRESIDENT 17-NOV-81 5000 10
  1. 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
  1. 7876 ADAMS CLERK 7788 23-MAY-87 1100 20
  1.  
  1. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
  1. ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
  1. 7900 JAMES CLERK 7698 03-DEC-81 950 30
  1. 7902 FORD ANALYST 7566 03-DEC-81 3000 20
  1. 7934 MILLER CLERK 7782 23-JAN-82 1300 10
  1.  
  1. 14 rows selected.
  2.  
  3. 来源: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. 《深入理解jvm》笔记---第六章

    类文件结构 1. Java一次编写,到处执行的基石:    Java编译产生的是字节码(bytecode).sun公司和其它虚拟机提供商公布各个平台上的虚拟机.这些虚拟机能够加载和执行这些与平台无关的 ...

  2. MySQL数据库优化详解(收藏)

    MySQL数据库优化详解 mysql表复制 复制表结构+复制表数据mysql> create table t3 like t1;mysql> insert into t3 select * ...

  3. Atitit 图像处理类库安装与安装模式的前世今生与未来大趋势attilax总结.docx

    Atitit 图像处理类库安装与安装模式的前世今生与未来大趋势attilax总结.docx 1. 安装的原理,主要是解压,复制,设置三大步1 2. 目前我们常见的三大种安装模式,二进制模式与源码安装模 ...

  4. 应该用bind+function取代虚函数吗?

    用bind+function取代虚函数在好几年前就有人提出了,曾引起广泛的讨论,有支持的有反对的,可能赞成的人占大多数.这个话题挺有趣,本来是作为技术沙龙的开放性话题来讨论的,由于时间关系并没有讨论. ...

  5. 对JSON格式的城市按照拼音首字母排序

    需求说明: App应用中最常见的一种操作就是对城市按照拼音首字母排序,以方便选择城市.而已有的json格式的城市数据是没有这种排序的. 已有的json格式的城市数据格式如下[简化之后]: 数据格式说明 ...

  6. 使用Python实现Map Reduce程序

    使用Python实现Map Reduce程序 起因 想处理一些较大的文件,单机运行效率太低,多线程也达不到要求,最终采用了集群的处理方式. 详细的讨论可以在v2ex上看一下. 步骤 MapReduce ...

  7. java double类型保留两位小数4种方法

    http://blog.csdn.net/huaishuming/article/details/17752365 ****************************************** ...

  8. Windows 安装 setuptools 和 feedparser

    一.安装setuptools: 页面: https://pypi.python.org/pypi/setuptools#downloads 1.下载该zip文件,解压,例如:C:\setuptools ...

  9. python 基础总计 2

    6.函数:      match.sqrt(),lower(),len(),type(),isinstance('a',str),max(),min(),dir(),hex(),setattar(ob ...

  10. [转]SSH和SSM对比总结

    原文地址:https://blog.csdn.net/peak_and_valley/article/details/52925032 当下流行的两种企业开发MVC开源框架,是我们Java程序猿必备知 ...