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;
/

最新文章

  1. MyBatis的一系列问题的处理(遍历Map集合和智能标签和属性和字段不一样的解决办法 和sql片段)(三)
  2. [原创]用windows7连接windows2003的终端服务器时,出现"由于这台计算机没有远程桌面客户端访问许可证,远程会话被中断"的问题
  3. openCV C++ 代码笔记
  4. ATM模拟器(附代码及运行结果)
  5. 用Spring Boot颠覆Java应用开发
  6. 手动安装python后,交互模式下退格键乱码
  7. C#FTP登入
  8. UIGestureRecongnizer 手势检测 swift
  9. Java之--Java基础知识
  10. less使用001
  11. 基于visual Studio2013解决算法导论之027hash表
  12. 【j2ee】div浮动层拖拽
  13. 简单理清一下proto与prototype
  14. Jmeter的安装和启动时出现unable to access jarfile apachejmeter.jar error value=1错误处理
  15. 19_04_19校内训练[Game]
  16. Eclipse中查看JDK类库源代码
  17. Unicode转字符串
  18. UGUI之控件以及按钮的监听事件系统
  19. python之daemon线程
  20. 解决boot空间不足问题

热门文章

  1. MKCOL not allowed
  2. TortoiseSVN,排除不想提交文件的方法(转)
  3. UIView属性及方法
  4. mysql命令行操作
  5. 如何优化tomcat配置(从内存、并发、缓存4个方面)优化
  6. jstl简介
  7. Verilog语法基础讲解之参数化设计
  8. telnet不通11211端口,防火墙
  9. Codeforces 743D:Chloe and pleasant prizes(树形DP)
  10. Greenplum迁移到配置不同的GP系统