A cursor acts logically as a pointer into a result set. You can move the cursor through the result set, processing each row, until you determine you are at the end of the result set. There are three types of syntax associated with cursors: creating the cursor, fetching with the cursor, and closing the cursor. In addition, there are a number of attributes of a cursor you can use in your logical comparisons. The following are the types of Cursors in Oracle:

Explicit Cursors

Explicit Cursors are cursors that you declare and use.

Implicit Cursors

PL/SQL allows you to include SQL statements, including SELECT statements, as a part of your code without declaring a cursor, that is called an implicit cursor.

Ref Cursors

A cursor references a result set. The REF CURSOR allows you to pass a cursor reference from one PL/SQL program unit to another. In other words, it allows you to create a variable that will receive a cursor and enable access to its result set, but in this blog I am giving examples for only Explicit and Implicit Cursors, I will give example for Ref Cursors and Dynamic Cursor in another blog.
 
An example of Explicit Cursor:
 
DECLARE
   nemployeeid   NUMBER;
   dstartdate    DATE;
   denddate      DATE;
   sjobid        VARCHAR2 (20);
 
   -- declare cursor
   CURSOR curjob
   IS
      SELECT employee_id,
             start_date,
             end_date,
             job_id
        FROM hr.job_history;
BEGIN
   OPEN curjob;
 
   LOOP
      FETCH curjob
      INTO nemployeeid, dstartdate, denddate, sjobid;
 
      EXIT WHEN curjob%NOTFOUND;
      DBMS_OUTPUT.put_line(   'Employee '
                           || nemployeeid
                           || 'had job '
                           || sjobid
                           || ' for '
                           || (denddate - dstartdate)
                           || ' days.');
   END LOOP;
 
   CLOSE curjob;
END;
/
Same example is given below for explicit cursor but with For Loop, the For Loop cursors are more smart as there is no need to declare variables to fetch values in them and no need to open or close or to check whether the pointer is at end of the cursor. Here is the example:
 
DECLARE
   CURSOR curjob
   IS
      SELECT employee_id,
             start_date,
             end_date,
             job_id
        FROM hr.job_history;
BEGIN
   FOR jh_rec IN curjob
   LOOP
      DBMS_OUTPUT.put_line(   '‘Employee '
                           || jh_rec.employee_id
                           || ' had job '
                           || jh_rec.job_id
                           || ' for '
                           || (  jh_rec.end_date
                               - jh_rec.start_date
                               || ' days.'));
   END LOOP;
END;
/
 
An Implicit Cursor example:
 
DECLARE
   nempno   NUMBER;
 
   CURSOR curjob
   IS
      SELECT employee_id,
             start_date,
             end_date,
             job_id
        FROM hr.job_history;
BEGIN
  -- below sql query is the type of Implicit Cursor
   SELECT COUNT ( * ) INTO nempno FROM hr.job_history;
 
   DBMS_OUTPUT.put_line (
      'There are ' || nempno || ' employee history records.');
 
   FOR jh_rec IN curjob
   LOOP
      DBMS_OUTPUT.put_line(   '‘Employee '
                           || jh_rec.employee_id
                           || ' had job '
                           || jh_rec.job_id
                           || ' for '
                           || (  jh_rec.end_date
                               - jh_rec.start_date
                               || ' days.'));
   END LOOP;
END;
/

Examples For PLSQL Cursors - Explicit, Implicit And Ref Cursors的更多相关文章

  1. C#类型转换运算符之 explicit implicit

    类型转换运算符 explicit和implicit用于声明用户定义的类型转换运算符,如果可以确保转换过程不会造成数据丢失,则可使用这两个关键字在用户定义的类型和其他类型之间进行转换. explicit ...

  2. Oracle PLSQL Demo - 20.弱类型REF游标[没有指定查询类型,也不指定返回类型]

    declare Type ref_cur_variable IS REF cursor; cur_variable ref_cur_variable; v_ename ); v_deptno ); v ...

  3. Oracle PLSQL Demo - 16.弱类型REF游标[没有指定查询类型,已指定返回类型]

    declare Type ref_cur_variable IS REF cursor; cur_variable ref_cur_variable; rec_emp scott.emp%RowTyp ...

  4. On Caching and Evangelizing SQL

    http://www.oracle.com/technetwork/issue-archive/2011/11-sep/o51asktom-453438.html   Our technologist ...

  5. C#中的Explicit和Implicit

    今天在Review一个老项目的时候,看到一段奇怪的代码. if (dto.Payment == null) continue; var entity = entries.FirstOrDefault( ...

  6. 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 ...

  7. Oracle PL/SQL Articles

    我是搬运工....http://www.oracle-base.com/articles/plsql/articles-plsql.php Oracle 8i Oracle 9i Oracle 10g ...

  8. 游标-Oracle游标汇总

    游标(Cursor):用来查询数据库,获取记录集合(结果集)的指针,可以让开发者一次访问一行结果集,在每条结果集上作操作.    游标可分为:    <!--[if !supportLists] ...

  9. Miscellaneous Articles

    标记一下,慢慢看  http://www.oracle-base.com/articles/misc/articles-misc.php Miscellaneous Articles DBA Deve ...

随机推荐

  1. TVideoGrabber如何将网络摄像头影像实时发布到网络

    在TVideoGrabber中如何将网络摄像头影像实时发布到网络?如何设置正在运行TVideoGrabber的一台电脑,同时通过另一台电脑在网络中实时的观看在线视频呢? 在这里称发送视频流的电脑为“m ...

  2. 【python cookbook】【数据结构与算法】8.与字典有关的计算问题

    问题:在字典上对数据执行各式各样的计算(比如求最小值.最大值.排序). 解决方案:利用zip()将字典的键-值对“反转”为值-键对序列. 例如:如下字典存放的股票名称和对应的价格: >>& ...

  3. Git 使用规范流程

    Git教程:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 团队开发中,遵循一个合 ...

  4. Nginx+Keepalived实现 转载

    一.Keepalived简介 keepalived是一个类似于layer3, 4 & 5交换机制的软件,也就是我们平时说的第3层.第4层和第5层交换.Keepalived的作用是检测web服务 ...

  5. Java编译那些事儿【转】

    转自:http://blog.csdn.net/lincyang/article/details/8553481 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 命令行编译 使 ...

  6. 华硕本本重装系统后出现can not open file c:\RECOVERY.DAT

    华硕本本重装系统后出现can not open file c:\RECOVERY.DAT很多网友会觉得困惑,不知道为什么会这样,下面我就为大家来解决这个问题,方法一: 这个问题就出在华硕自带系统都是装 ...

  7. 使用python脚本监控weblogic

    1.python的脚本如下: ############################################################################### #crea ...

  8. ecshop微信支付(0923更新)商户支付密钥key的生成与设置

    ECSHOP 微信支付(0923更新)商户支付密钥key的生成与设置 说明:新版微信支付,用户必须授权登录才能支付.需要商家自己设置商户号支付密钥. 申请微信支付手机版部分时需要填写的配置接口地址: ...

  9. [充电][ios]ios充电接口

    知乎推荐:http://www.zhihu.com/topic/19693633/top-answers 外文教学网站: http://www.raywenderlich.com/ 著作权归作者所有. ...

  10. ACM题目————二叉树的遍历

    一.二叉树的后序遍历: 题目描述 给定一颗二叉树,要求输出二叉树的深度以及后序遍历二叉树得到的序列.本题假设二叉树的结点数不超过1000 输入 输 入数据分为多组,第一行是测试数据的组数n,下面的n行 ...