游标是SQL的一个内存工作区,由系统或用户以变量的形式定义。游标的作用就是用于临时存储从数据库中提取的数据块。Oracle数据库的Cursor类型包含三种: 静态游标:分为显式(explicit)游标和隐式(implicit)游标;REF游标:是一种引用类型,类似于指针。下面我们一一介绍它们的使用。

1.隐式游标

1)Select …INTO…语句,DML语句,使用隐式Cursor。此外,还有一种使用FOR LOOP的Implicit Cursor用法。

2)可以通过隐式Cusor的属性来了解操作的状态和结果。Cursor的属性包含:

SQL%ROWCOUNT 整型代表DML语句成功执行的数据行数。

SQL%FOUND  布尔型值为TRUE代表插入、删除、更新或单行查询操作成功。

SQL%NOTFOUND 布尔型与SQL%FOUND属性返回值相反。

SQL%ISOPEN 布尔型DML执行过程中为真,结束后为假。

3) 隐式Cursor由系统自动打开和关闭.

例如:

set serveroutput on

declare

begin

update employees setemployee_name='Mike'whereemployee_id=1001;

if SQL%FOUND then

dbms_output.put_line('Name is updated');

else

dbms_output.put_line('Name is not updated');

end if;

end;

/

set serveroutput on

declare

begin

for tableInfo in (select * from user_tables) loop

dbms_output.put_line(tableInfo.table_name);

end loop;

exception

when others then

dbms_output.put_line(sqlerrm);

end;

/

2.显式游标

1) 显式Cursor的属性包含:

游标的属性   返回值类型   意义

%ROWCOUNT   整型  获得FETCH语句返回的数据行数

%FOUND  布尔型 最近的FETCH语句返回一行数据则为真,否则为假

%NOTFOUND   布尔型 与%FOUND属性返回值相反

%ISOPEN 布尔型 游标已经打开时值为真,否则为假

2) 对于显式游标的运用分为四个步骤:

a 定义游标---Cursor  [Cursor Name]  IS;

b 打开游标---Open  [Cursor Name];

c  操作数据---Fetch  [Cursor name]

d  关闭游标---Close [Cursor Name]

以下是几种常见显式Cursor用法。

set serveroutput on

declare

cursor cur is select * from user_tables;

tableInfo user_tables%rowtype;

begin

open cur;

loop

fetch cur into tableInfo;

exit when cur%notfound;

dbms_output.put_line(tableInfo.table_name);

end loop;

exception

when others then

dbms_output.put_line(sqlerrm);

close cur;

end;

/

set serveroutput on

declare

cursor cur is select * from user_tables;

begin

for tableInfo in cur loop

dbms_output.put_line(tableInfo.table_name);

end loop;

exception

when others then

dbms_output.put_line(sqlerrm);

end;

/

还可以使用带参数open的cursor。

set serveroutput on

declare

cursor cur(tblName varchar2) is select * from user_constraints wheretable_name=tblName;

tableInfo user_constraints%rowtype;

begin

open cur('EMPLOYEES');

loop

fetch cur into tableInfo;

exit when cur%notfound;

dbms_output.put_line(tableInfo.constraint_name);

end loop;

exception

when others then

dbms_output.put_line(sqlerrm);

close cur;

end;

/

set serveroutput on

declare

cursor cur(tblName varchar2) is select * from user_constraints wheretable_name=tblName;

begin

for tableInfo in cur('EMPLOYEES') loop

dbms_output.put_line(tableInfo.constraint_name);

end loop;

exception

when others then

dbms_output.put_line(sqlerrm);

end

/

可以使用WHERE CURRENT OF子句执行UPDATE或DELETE操作。

set serveroutput on

declare

cursor cur is select * from employees for update;

begin

for tableInfo in cur loop

update employees setsalarysalary=salary*1.1 where current of cur;

end loop;

commit;

exception

when others then

dbms_output.put_line(sqlerrm);

end;

/

3.REF CURSOR(Cursor Variables)

REF Cursor在运行的时候才能确定游标使用的查询。利用REF CURSOR,可以在程序间传递结果集(一个程序里打开游标变量,在另外的程序里处理数据)。

也可以利用REF CURSOR实现BULK SQL,提高SQL性能。

REF CURSOR分两种,Strong REF CURSOR 和 Weak REF CURSOR。

Strong REF CURSOR:指定retrun type,CURSOR变量的类型必须和return type一致。

Weak REF CURSOR:不指定return type,能和任何类型的CURSOR变量匹配。

Ref cursor的使用:

1) Type [Cursor type name] is ref cursor

2) Open cursor for...

3) Fetch  [Cursor name]

4) Close Cursor

例如:

Step1:

create or replace package TEST as

type employees_refcursor_type is ref cursor return employees%rowtype;

procedure employees_loop(employees_cur IN employees_refcursor_type);

end TEST;

/

Step2:

create or replace package body TEST as

procedure employees_loop(employees_cur IN employees_refcursor_type) is

emp employees%rowtype;

begin

loop

fetch employees_cur into emp;

exit when employees_cur%NOTFOUND;

dbms_output.put_line(emp.employee_id);

end loop;

end employees_loop;

end TEST;

/

Step3:

set serveroutput on

declare

empRefCur TEST.employees_refcursor_type;

begin

for i in 10..20 loop

dbms_output.put_line('DepartmentID=' || i);

open empRefCur for select * from employees wheredepartment_id=i;

TEST.employees_loop(empRefCur);

end loop;

exception

when others then

dbms_output.put_line(sqlerrm);

close empRefCur;

end;

/

4.BULK SQL

使用FORALL和BULK COLLECT子句。利用BULK SQL可以减少PLSQL Engine和SQL Engine之间的通信开销,提高性能。

1. To speed up INSERT, UPDATE, and DELETE statements, enclose the SQL statement within a PL/SQL FORALL statement instead of a loop construct. 加速INSERT, UPDATE, DELETE语句的执行,也就是用FORALL语句来替代循环语句。

2. To speed up SELECT statements, include the BULK COLLECT INTO clause in the SELECT statement instead of using INTO.  加速SELECT,用BULK COLLECT INTO 来替代INTO。

SQL>create table employees_tmp as select first_name, last_name, salary from employees where0=1;

set serveroutput on

declare

cursor employees_cur(depId employees.department_id%type) is select first_name, last_name, salary from employees wheredepartment_id=depId;

type employee_table_type is table of employees_cur%rowtype index by pls_integer;

employee_table employee_table_type;

begin

open employees_cur(100);

fetch employees_cur bulk collect into employee_table;

close employees_cur;

for i in 1..employee_table.count loop

dbms_output.put_line(employee_table(i).first_name || ' ' || employee_table(i).last_name || ',' || employee_table(i).salary);

end loop;

forall i in employee_table.first..employee_table.last

insert into employees_tmp values(employee_table(i).first_name, employee_table(i).last_name, employee_table(i).salary);

commit;

end;

/

5.  动态性能表V$OPEN_CURSOR

本视图列出session打开的所有cursors。

关于Oracle数据库游标的类型和使用的知识就介绍到这里了,如果您想了解更多的Oracle数据库的知识,可以到这里看一下:http://database.51cto.com/oracle/,相信一定能够带给您收获的!

【编辑推荐】

【责任编辑:赵鹏 TEL:(010)68476606】

点赞 0

oracle利用游标添加数据库,Oracle数据库游标的类型及使用实例全解相关推荐

  1. oracle游标添加数据,Oracle使用游标更新数据

    1. 使用游标修改数据 定义一个游标,游标名称为 mycursor 更新scott用户中emp表中empno为7369的销售额 -- Created on 2015/11/30 by ZHANW de ...

  2. ado.net能访问oracle,利用ADO.NET访问Oracle数据库的实现

    1.前言随着互联网的发展,人们对Web网页的要求越来越多的趋向于动态交互性,而这很大程度上依赖于Web数据库.因为动态交互需要强大的信息系统支持,信息数据以数据库形式表示更容易更新和管理.通过网页如何 ...

  3. MySQL数据库死锁了,该怎么办?一文全解最新教程

    文章目录 正文 死锁的发生 为什么会产生死锁? Insert 语句是怎么加行级锁的? 1.记录之间加有间隙锁 2.遇到唯一键冲突 如何避免死锁? 之前分享过 MySQL 死锁的文章,然后很多读者对「插 ...

  4. oracle利用游标添加数据库,Oracle游标的使用实例详解

    什么是游标? ①从表中检索出结果集,从中每次指向一条记录进行交互的机制. ②关系数据库中的操作是在完整的行集合上执行的. 由 SELECT 语句返回的行集合包括满足该语句的 WHERE 子句所列条件的 ...

  5. oracle 游标查询数据库,Oracle数据库使用游标查询结果集所有数据

    --Oracle使用游标查询结果集所有数据 DECLARE myTabelName NVARCHAR2(200):=''; --表名 myTableRowComment NVARCHAR2(200): ...

  6. 13、oracle数据库下的游标

    ORACLE下的游标操作 游标是sql的一个内存工作区,由系统或者用户以变量的形式定义.游标的作用是用于临时存储从数据库中提取的数据块.游标有静态游标.动态游标之分,静态游标又可分为隐式游标和显式游标 ...

  7. oracle使用游标insert数据库,数据库游标使用之oracle游标

    游标(cursor)的定义:数据库系统在内存中开设的一个数据缓冲区,存放SQL语句的执行结果,通过fecth用于定位结果集的行 和 遍历结果集. oracle数据库的游标分为:静态游标(隐式和显式)和 ...

  8. myeclipse添加oracle,向MyEclipse添加Oracle数据库

    向MyEclipse添加Oracle数据库 1.点击下面圈起来的位置,打开MyEclipse database Explorer视图. 2.在如图空白处右击,选择new进入New Database C ...

  9. oracle数据库insert into,oracle中insert into用法 oracle中insert如何带条件添加数据?

    oracle insert into 脚本怎么写 INSE INTO BOOK(bookid,name,price) VALUES('100123','oracle ',54); 或者 INSE IN ...

最新文章

  1. 内存溢出和内存泄漏的定义,产生原因以及解决方法(面试经验总结)
  2. mysql select 返回列,是否可以对在mysql SELECT语句中返回列的顺序进行排序?
  3. 最火的前端开发框架Bootstrap使用教程学习!
  4. [异常解决] 安卓6.0权限问题导致老蓝牙程序出现异常解决办法:Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission...
  5. ios nslinkattributename 自定义url_iOS音视频播放指南(二)
  6. 【数据结构】图的深度优先搜索
  7. Halcon算子翻译——default
  8. 各国“未雨绸缪”致网络安全陷入困境 安全防御诱发“防御性入侵”
  9. 大学excel题库含答案_大学生计算机基础excel试题及答案
  10. 数据的结构分类:结构化数据,半结构化数据以及非结构化数据
  11. 软件工程的未来发展趋势
  12. 有什么画ER关系比较好用的软件图?
  13. 三星发布全球首款太阳能笔记本
  14. Javascript Prototype污染攻击(原型链污染,Bugku-web-sodirty wp)
  15. 南宁市第二十六中学:教研路漫漫,花香伴我行
  16. 用一部极客电影让你感受互联网科技的潜力
  17. 动态链表的创建、节点内存空间申请以及释放
  18. Apache Kylin CUBE 剪枝优化和cuboid数量计算公式总结
  19. 关于力控无法插入图片
  20. 添加右键菜单:用xx打开(管理员身份下,也可以运行)【解决 ShellExecute failed (2): Is this command correct? 的问题】

热门文章

  1. 教你如何将二进制文件导入到数据库
  2. 工商银行:应用多k8s集群管理及容灾实践
  3. 华为与五粮液签署战略合作协议
  4. 补习系列(8)-springboot 单元测试之道
  5. 用Anaconda3搭建自己的TensorFlow环境
  6. java 注解: Annotation
  7. 扩展、统计线性化和无迹RTS平滑器
  8. C++函数分文件编写
  9. 【李宏毅机器学习】Recurrent Neural Network Part2 循环神经网络(p21) 学习笔记
  10. 把数据或是numpy数据转换为keras张量