--演示隐式游标,系统自动声明,自动打开,自动使用并且自动关闭

begin

     update emp set sal = 1000;

     dbms_output.put_line('影响的行数:' || sql%rowcount);

end;

 

rollback;

 

/*游标的使用方法:

第一步:声明游标

第二步:打开游标

第三步:使用游标进行循环操作

第四步:关闭游标*/

 

--普通游标,游标本身就是一个变量

declare     

     --下面的这行代码声明了一个游标

     cursor mycur is select * from emp where deptno = 20;

     emprow emp%rowtype;

begin

     open mycur; --打开游标

     loop

         fetch mycur into emprow; --把游标所指的纪录放到变量中

         exit  when (mycur%notfound); --当游标没有指向行时退出循环

         dbms_output.put_line('名字:' || emprow.ename || '薪水:' || emprow.sal);

     end loop;

     close mycur;  --关闭游标

end;

 

--简单游标,列操作

declare

       empname emp.ename%type;

       empsal emp.sal%type;

       cursor mycur is select ename,sal from emp where deptno = 30;

begin

     open mycur;

     loop

         fetch mycur into empname,empsal;

         exit when mycur%notfound;

         dbms_output.put_line('姓名:' || empname || '工资' || empsal);

     end loop;

end;

 

--简单游标,列操作

declare

  cursor c 

  is

  select * from dept;

  vDept_row_record c%rowtype;

begin

  open c;

  fetch c into vDept_row_record;

  dbms_output.put_line(vDept_row_record.dname);

  close c;

end;

 

--when循环游标

declare

  cursor c 

  is

  select * from dept;

  vDept_row_record c%rowtype;

begin

  open c;

  loop

       fetch c into vDept_row_record;

       exit when(c%notfound);

       dbms_output.put_line(vDept_row_record.dname);

  end loop;

  close c;

end;

 

--while循环游标

declare

  cursor c

  is

  select * from dept;

  vDept_row_record c%rowtype;

begin

  open c;

  fetch c into vDept_row_record;

  while (c%found) loop

    dbms_output.put_line(vDept_row_record.dname);

    fetch c into vDept_row_record;

  end loop;

  close c;

end;

 

--for循环游标

declare

  cursor c

  is

  select * from dept;

  vDept_row_record c%rowtype;

begin

  for vDept_row_record in c loop

    dbms_output.put_line(vDept_row_record.dname);

  end loop;

end;

 

--带参游标

declare

  cursor c(sSal emp.sal%type, sEmpno emp.empno%type)

  is

  select * from emp where sal >= sSal and empno > sEmpno;

begin

  for record_data in c(2500, 6666) loop

      dbms_output.put_line(record_data.ename);

  end loop;

end;

 

--update游标

declare

  cursor c(sSal emp2.sal%type)

  is

  select * from emp2 where sal >= sSal for update;

begin

  for record_data in c(2500) loop

    if (record_data.sal < 3000) then

      update emp2 set sal = sal + 3 where current of c;

      dbms_output.put_line(record_data.ename);

    elsif (record_data.sal = 5000) then

      update emp2 set sal = sal - 3 where current of c;

      dbms_output.put_line(record_data.ename);

    end if;

  end loop;

end;

 

--引用游标不能使用循环游标的语法

--引用游标不能进行删除和修改

--引用游标是一个数据类型,使用该类型必须声明变量

 

--弱类型引用游标,就是不指定游标将要提取的数据行的类型

declare

       type my_cur_type is ref cursor;

       mycur my_cur_type;--声明变量

       which varchar2(10);

       deptrow dept%rowtype;

       emprow emp%rowtype;

begin

     which := '&请选择dept还是emp';

     if (which = 'dept') then

        open mycur for select * from dept;

        loop

            fetch mycur into deptrow;

            exit when (mycur%notfound);

            dbms_output.put_line(deptrow.deptno || '  ' || deptrow.dname);

        end loop;

     elsif (which = 'emp') then

        open mycur for select * from emp;

        loop

            fetch mycur into emprow;

            exit when (mycur%notfound);

            dbms_output.put_line(emprow.empno || '  ' || emprow.ename);

        end loop;

     end if;

     close mycur;

end;

 

--强类型引用游标,就是指定游标将要提取的数据行的类型 ,只能是record或%rowtype类型

--比如:return number是错的,return emp.ename%type也是错的

declare

       type mycurtype is ref cursor return emp%rowtype;

       mycur mycurtype;--声明变量

       emprow emp%rowtype;

begin

        open mycur for select * from emp;

        loop

            fetch mycur into emprow;

            exit when mycur%notfound;

            dbms_output.put_line(emprow.empno || '  ' || emprow.ename);

        end loop;

        close mycur;

end;

Oracle笔记 九、PL/SQL 游标的使用的更多相关文章

  1. Oracle数据库之PL/SQL游标

    1. 游标概念 字面意思是游动的光标,是指向上下文区域的句柄或指针. 在PL/SQL块中执行CRUD操作时,ORACLE会在内存中为其分配上下文区.用数据库语言来描述游标就是:映射在上下文区结果集中一 ...

  2. Oracle学习笔记之五,Oracle 11g的PL/SQL入门

    1. PL/SQL概述 PL/SQL(Procedural Language/SQL)是Oracle的专用语言,是对标准SQL语言的扩展,它允许在其内部嵌套普通的SQL语句,还可以定义变量和常量,允许 ...

  3. oracle数据库之PL/SQL 块结构和组成元素

    一.PL/SQL 块 (一)PL/SQL 程序由三个块组成,即声明部分.执行部分.异常处理部分 PL/SQL 块的结构如下: 1.DECLARE /* 声明部分: 在此声明 PL/SQL 用到的变量, ...

  4. 每周一书《Oracle 12 c PL(SQL)程序设计终极指南》

    本周为大家送出的书是<Oracle 12 c PL(SQL)程序设计终极指南>,此书由机械工业出版社出版, 孙风栋,王澜,郭晓惠 著. 内容简介: <Oracle 12c PL/SQ ...

  5. Oracle数据库之PL/SQL包

    Oracle数据库之PL/SQL包 1. 简介 包(PACKAGE)是一种数据对象,它是一组相关过程.函数.变量.常量和游标等PL/SQL程序设计元素的组合,作为一个完整的单元存储在数据库中,用名称来 ...

  6. Oracle数据库之PL/SQL异常处理

    Oracle数据库之PL/SQL异常处理 异常指的是在程序运行过程中发生的异常事件,通常是由硬件问题或者程序设计问题所导致的. PL/SQL程序设计过程中,即使是写得最好的程序也可能会遇到错误或未预料 ...

  7. Oracle数据库之PL/SQL程序设计简介

    PL/SQL程序设计简介 一.什么是PL/SQL? PL/SQL是 Procedure Language & Structured Query Language 的缩写. ORACLE的SQL ...

  8. Oracle11g R2学习系列 之九 PL/SQL语言

    这是个重头戏,如果精通了PL/SQL,毫不夸张的说明精通了Oracle了.PL/SQL由以下三个部分组成(Definition,Manipulation,Control): DDL:数据定义语言,Cr ...

  9. PL/SQL 游标

    本随笔不是原创,只是学习笔记,用于加深记忆,原创地址PL/SQL --> 游标 一.游标的相关概念和特性 1.定义: 映射到结果集中的某一行的特定位置,类似与C语言中的指针.即通过游标方式定位到 ...

  10. oracle系列(四)PL/SQL

    过程,函数,触发器是PL/SQL编写的,存储在oracle中的.PL/SQL是非常强大的数据库过程语言. PL/SQL优点:性能,模块化,网络传输量,安全性缺点:移植性不好 简单分类:块:过程,函数, ...

随机推荐

  1. PLSQL_基础系列09_时间戳记TIMESTAMP(案例)

    2013-11-09 Created By BaoXinjian

  2. AP_AP系列 - 付款管理分析(案例)

    2014-07-08 Created By BaoXinjian 一.摘要 1. 付款 2. 发票付款概述 3. 使用发票工作台付款 4. 使用付款管理器付款 5. 银行账户模型 二.流程分析 1. ...

  3. file_get_content和curl的性能比较

    今天在获取微信一张二维码图片时发现使用php中的file_get_content方式和curl方式竟然相差了50倍左右,直接晕倒!!!

  4. 如何刪除GitHub中的repository

    如何刪除一github中的repository,這本該是個非常簡單的操作,可一開始搜的時候,有不少文章比較含糊.這裡就記錄下來吧. 1.訪問https://github.com/settings/pr ...

  5. Android开发者需要面对的8大挑战

    移动开发变得越来越受欢迎,但移动开发者正面临着一系列挑战.本文将介绍的是Android开发者需要面对的8个不利因素,例如缺乏硬件标准化,以及软件碎片.为Android OS开发app,给予了开发人员极 ...

  6. gRPC 的 RoadMap 20160325 更新

    gRPC是一个高性能.通用的开源RPC框架,其由Google主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf(Protocol Buffers)序列化协议开发,且支持众多开发 ...

  7. Django 的 CSRF 保护机制

    转自:http://www.cnblogs.com/lins05/archive/2012/12/02/2797996.html 用 django 有多久,我跟 csrf 这个概念打交道就有久了. 每 ...

  8. JavaScript对象的创建总结

    方式 缺点 优点 基于已有对象扩充属性和方法 不可重用,没有约束 无 工厂方法 检测不出是什么的实例 简单封装,可以传参 构造方法 每创建一个对象就有开辟存放方法的空间 能通过instanceof检测 ...

  9. winform异步进度条LongTime

    winform异步进度条LongTime,运用到回调函数 定义事件的参数类: namespace LongTime.Business { // 定义事件的参数类 public class ValueE ...

  10. 技术转载:Jni学习四:如何编写jni方法

    转载:http://blog.chinaunix.net/u1/38994/showart_1099528.html 一.概述: 在这篇文章中将会简单介绍如何编制一些简单的JNI 方法.我们都知道JN ...