cursor分为三种,一是直接声明为cursor变量,二是首先声明类型再声明变量,三是声明为sys_refcursor。

(1)直接声明

declare

cursor emp_cur  is select *  from emp;

emp_record emp%rowtype;

begin

open emp_cur;

loop

fetch emp_cur  into emp_record;

exit when  emp_cur%notfound;

dbms_output.put_line('name is:' || emp_record.ename ||' and sal is:' || emp_record.sal);

end loop;

close emp_cur;

end;

/

(2)ref cursor:分为强类型(有return子句的)和弱类型,强类型在使用时,其返回类型必须和return中的类型一致,否则报错,而弱类型可以随意打开任何类型。

例如:

强类型

declare

type emp_cur_type  is ref cursor return emp%rowtype;

emp_cur emp_cur_type;

emp_record emp%rowtype;

begin

open emp_cur  for select *  from  emp;

loop

fetch emp_cur  into emp_record;

exit when emp_cur%notfound;

dbms_output.put_line('name is:' ||  emp_record.ename || ' and sal is:' || emp_record.sal);

end loop;

close emp_cur;

--open emp_cur for select * from dept; 错误的,类型不一致。

--close emp_cur;

end;

/

弱类型:

declare

type emp_cur_type is ref cursor;

emp_cur emp_cur_type;

emp_record emp%rowtype;

dept_record dept%rowtype;

begin

open emp_cur for select *  from emp;

loop

fetch emp_cur into emp_record;

exit when emp_cur%notfound;

dbms_output.put_line('name is:' || emp_record.ename || ' and sal is:' || emp_record.sal);

end loop;

close emp_cur;

open emp_cur  for select *  from dept; --可再次打开,不同类型的

loop

fetch emp_cur  into dept_record;

exit when emp_cur%notfound;

dbms_output.put_line('dname is:' || dept_record.dname);

end loop;

close emp_cur;

end;

/

(3)sys_refcursor:可多次打开,直接声明此类型的变量,不用先定义类型再声明变量。

declare

emp_cur sys_refcursor;

emp_record emp%rowtype;

dept_record dept%rowtype;

begin

open emp_cur for select *  from emp;

loop

fetch emp_cur  into emp_record;

exit when emp_cur%notfound;

dbms_output.put_line('name is:' || emp_record.ename || ' and sal is:' || emp_record.sal);

end loop;

close emp_cur;

open emp_cur  for select *  from dept; --可再次打开,不同类型的

loop

fetch emp_cur  into dept_record;

exit when emp_cur%notfound;

dbms_output.put_line('dname is:' || dept_record.dname);

end loop;

close emp_cur;

end;

/

其他总结:

1、游标可以用for循环,但只限于cursor cur_var is ……这种类型,用在其他的里面都是错误的;for本身就包含了打开、关闭游标,此时再显示打开关闭都是错误的。

declare

cursor emp_cur is select *  from emp;

begin

--open emp_cur; 是错误的,因为for本身就包含了打开、关闭

for emp_record in emp_cur

loop

dbms_output.put_line('name is:' ||  emp_record.ename || ' and sal is:' || emp_record.sal);

end loop;

--close emp_cur; 是错误的,for本身包含了关闭。

end;

--是不是表示:ref cursor变量不支持for打开并循环?

declare

type emp_cur_type  is ref cursor return emp%rowtype;

emp_cur emp_cur_type;

begin

open emp_cur  for select *  from emp;--怎么都是错,for已经打开了。

for emp_record  in emp_cur -- 不管前面有没有打开语句,for都不承认这种类型

loop

dbms_output.put_line('name is:' ||  emp_record.ename || ' and sal is:' || emp_record.sal);

end loop;

end;

2、游标可以带参数

DECLARE

CURSOR c1 (job VARCHAR2, max_wage NUMBER) IS

SELECT * FROM employees  WHERE job_id = job  AND salary > max_wage;

BEGIN

FOR person  IN  c1('CLERK', 3000)

LOOP

DBMS_OUTPUT.PUT_LINE('Name = ' || person.last_name || ', salary = ' ||

person.salary || ', Job Id = ' || person.job_id );

END LOOP;

END;

3、bulk collect批量赋值

declare

type emp_cur_type  is ref cursor;

emp_cur emp_cur_type;

type name_list is table of emp.ename%type;

type sal_list  is table of emp.sal%type;

names name_list;

sals sal_list;

begin

open emp_cur for select ename,sal from emp;

fetch emp_cur bulk collect into names,sals;

close emp_cur;

for i  in names.first .. names.last

loop

dbms_output.put_line('name is:'||names(i)||' and sal is:'||sals(i));

end loop;

end;

/

4、cursor变量的位置

CREATE PACKAGE emp_data AS

TYPE EmpCurTyp IS REF CURSOR RETURN employees%ROWTYPE;

-- emp_cv EmpCurTyp; -- not allowed

PROCEDURE open_emp_cv;

END  emp_data;

/

CREATE PACKAGE BODY emp_data  AS

-- emp_cv EmpCurTyp; -- not allowed

PROCEDURE open_emp_cv  IS

emp_cv EmpCurTyp; -- this is legal

BEGIN

OPEN emp_cv  FOR SELECT *  FROM  employees;

END  open_emp_cv;

END  emp_data;

/

5、嵌套cursor

打开父cursor时,子cursor隐含打开;当

语法格式:cursor(subquery)

A   nested  cursor  is  implicitly  opened when  the  containing  row  is  fetched from  the  parent  cursor.

The  nested  cursor  is  closed  only  when:

The  nested  cursor  is  explicitly  closed by  the  user

The  parent  cursor  is  reexecuted

The  parent  cursor  is  closed

The  parent  cursor  is  canceled

示例;
declare

type emp_cur_type  is ref cursor ;

type dept_cur_type is ref cursor ;

v_ename emp.ename%type;

v_dname dept.dname%type;

emp_cur emp_cur_type;

dept_cur dept_cur_type;

begin

open dept_cur  for

select d.dname,

cursor(select  e.ename  from  emp  e  where e.deptno=d.deptno )emps

from dept d;

loop

fetch dept_cur into v_dname,emp_cur;

exit when dept_cur%notfound;

dbms_output.put_line('dname is : '||v_dname);

loop

fetch emp_cur into  v_ename;

exit when emp_cur%notfound;

dbms_output.put_line('--ename is : '||v_ename);

end  loop;

end  loop;

close  dept_cur;

end;

Oracle Cursor用法总结的更多相关文章

  1. ORACLE RETURNING 用法总结

    ORACLE RETURNING 用法总结 场景 在存储过程.PL/SQL块里需要返回INSERT.DELETE.UPDATE.MERGE等DML语句执行后的信息时使用,合理使用returning能够 ...

  2. Oracle触发器用法实例详解

    转自:https://www.jb51.net/article/80804.htm. 本文实例讲述了Oracle触发器用法.分享给大家供大家参考,具体如下: 一.触发器简介 触发器的定义就是说某个条件 ...

  3. [转载]Oracle触发器用法实例详解

    本文实例讲述了Oracle触发器用法.分享给大家供大家参考,具体如下: 一.触发器简介 触发器的定义就是说某个条件成立的时候,触发器里面所定义的语句就会被自动的执行. 因此触发器不需要人为的去调用,也 ...

  4. Oracle instr用法

    1:实现indexOf功能,.从第1个字符开始,搜索第1次出现子串的位置 ,) as i from dual; select instr('oracle','or') as i from dual; ...

  5. Oracle minus用法详解及应用实例

    本文转载:https://blog.csdn.net/jhon_03/article/details/78321937 Oracle minus用法 “minus”直接翻译为中文是“减”的意思,在Or ...

  6. ORACLE SEQUENCE用法(转)

    ORACLE SEQUENCE用法 在oracle中sequence就是序号,每次取的时候它会自动增加.sequence与表没有关系. 1.Create Sequence     首先要有CREATE ...

  7. Oracle cursor学习笔记

    目录 一.oracle库缓存 1.1.库缓存简介 1.2.相关概念 1.3.库缓存结构 1.4.sql执行过程简介 二.oracle cursor 2.1.cursor分类 2.2.shared cu ...

  8. Oracle数据库用法汇总

    一些Oracle数据库用法的小总结 1.使用insert into创建新表 insert into destdb.sub_contract (userid,contractid) select msi ...

  9. oracle中REF Cursor用法

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

随机推荐

  1. 中文目录对 sublime text 有什么影响?

    用了这软件好几个月了,一直没出现问题.最近做精简时,发现一个奇怪的问题. 相同的配置,为什么两个程序表现得不一样? 难道是哪里的配置不一样? 难道是插件被我精简得太厉害了? 难道是插件有依赖文件被我删 ...

  2. 使用R语言的RTCGA包获取TCGA数据--转载

    转载生信技能树 https://mp.weixin.qq.com/s/JB_329LCWqo5dY6MLawfEA TCGA数据源 - R包RTCGA的简单介绍 - 首先安装及加载包 - 指定任意基因 ...

  3. 《spring boot 实战》读书笔记

    前言:虽然已经用spring boot开发过一套系统,但是之前都是拿来主义,没有系统的,全面的了解过这套框架.现在通过学习<spring boot实战>这本书,希望温故知新.顺便实现自己的 ...

  4. 实现一个优先级队列,每次pop 返回优先级最高的元素

    demo1 实现一个按优先级排序的队列, 并且在这个队列上面每次 pop 操作总是返回优先级最高的那个元素 import heapq class PriorityQueue: def __init__ ...

  5. Jmeter 老司机带你一小时学会Jmeter

    Jmeter的安装   官网下载地址:http://jmeter.apache.org/download_jmeter.cgi 作为Java应用,是需要JDK环境的,因此需要下载安装JAVA,并且作必 ...

  6. C++ Web 编程

    C++ Web 编程 什么是 CGI? 公共网关接口(CGI),是一套标准,定义了信息是如何在 Web 服务器和客户端脚本之间进行交换的. CGI 规范目前是由 NCSA 维护的,NCSA 定义 CG ...

  7. &&并且, ||或 , 的用法 ,区别

    &&与运算必须同时都为true才是true,如果左边为false结果肯定为false: ||或运算,只要左边为true结果一定为true,两边都为false结果才是false. 只有当 ...

  8. Android主页Activity对多个Fragment实现不同的沉浸式标题(图片或者文字标题)

    提示:讲解的该例实现是 FragmentTabHost + Fragment 实现: 1.示例效果图: 2.场景需求: 如示例图所示,在首页实现轮播图的沉浸,而 “发现” 和“我的”页是标题的沉浸. ...

  9. try catch对Spring事务的影响

    一.Spring 的默认事务机制,当出现unchecked异常时候回滚,checked异常的时候不会回滚. 异常中unchecked异常包括error和runtime异常.需要try catch或向上 ...

  10. 《剑指offer》第五十六题(数组中唯一只出现一次的数字)

    // 面试题56(二):数组中唯一只出现一次的数字 // 题目:在一个数组中除了一个数字只出现一次之外,其他数字都出现了三次.请 // 找出那个吃出现一次的数字. #include <iostr ...