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

--测试数据

create table student(sno number primary key,sname varchar2(10));

declare i number:=1;
begin
while i<=50
loop
insert into student(sno,sname) values (i,'name'||to_char(i));
i:=i+1;
end loop;
end;

隐式游标属性:
SQL%ROWCOUNT 整型代表DML语句成功执行的数据行数。
SQL%FOUND 布尔型值为TRUE代表插入、删除、更新或单行查询操作成功。
SQL%NOTFOUND 布尔型与SQL%FOUND属性返回值相反。
SQL%ISOPEN 布尔型DML执行过程中为真,结束后为假。

declare
begin
update student set sname ='name'||to_char(sno*10) where sname='name80';
if sql%found then
dbms_output.put_line('name is updated');
else
dbms_output.put_line('没有记录');
end if;
end; declare
begin
for names in (select * from student) loop
dbms_output.put_line(names.sname);
end loop;
exception when others then
dbms_output.put_line(sqlerrm);
end;

显式游标属性:
%ROWCOUNT 获得FETCH语句返回的数据行数。
%FOUND 最近的FETCH语句返回一行数据则为真,否则为假。
%NOTFOUND 布尔型 与%FOUND属性返回值相反。
%ISOPEN 布尔型 游标已经打开时值为真,否则为假。

对于显式游标的运用分为四个步骤:
a 定义游标 --- Cursor [Cursor Name] IS;
b 打开游标 --- Open [Cursor Name];
c 操作数据 --- Fetch [Cursor name];
d 关闭游标 --- Close [Cursor Name];

典型显式游标:

declare cursor cur_rs is select * from student;
sinfo student%rowtype;
begin
open cur_rs;
loop
fetch cur_rs into sinfo;
exit when cur_rs%%notfound;
dbms_output.put_line(sinfo.sname);
end loop;
exception when others then
dbms_output.put_line(sqlerrm);
end;

带参数open的显式cursor:

declare cursor cur_rs(in_name varchar2) is select * from student where sname=in_name;
begin
for sinfo in cur_rs('sname') loop
dbms_output.put_line(sinfo.sname);
end loop;
exception when others then
dbms_output.put_line(sqlerrm);
end;

使用current of语句执行update或delete操作:

declare
cursor cur_rs is select * from student for update;
begin
for sinfo in cur_rs loop
update student set sname=sname||'xx' where current of cur_rs;
end loop;
commit;
exception when others then
dbms_output.put_line(sqlerrm);
end;

REF游标,用于处理运行时才能确定的动态sql查询结果,利用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变量匹配。

运行时根据动态sql查询结果遍历:

create or replace package pkg_test01 as
type student_refcursor_type is ref cursor return student%rowtype;
procedure student_rs_loop(cur_rs IN student_refcursor_type);
end pkg_test01; create or replace package body pkg_test01 as
procedure student_rs_loop(cur_rs IN student_refcursor_type) is
std student%rowtype;
begin
loop
fetch cur_rs into std;
exit when cur_rs%NOTFOUND;
dbms_output.put_line(std.sname);
end loop;
end student_rs_loop;
end pkg_test01; declare stdRefCur pkg_test01.student_refcursor_type;
begin
for i in 10..50 loop
dbms_output.put_line('Student NO=' || i);
open stdRefCur for select * from student where sno=i;
pkg_test01.student_rs_loop(stdRefCur);
end loop;
exception when others then dbms_output.put_line(sqlerrm);
close stdRefCur;
end;

使用FORALL和BULK COLLECT子句。利用BULK SQL可以减少PLSQL Engine和SQL Engine之间的通信开销,提高性能。
1.加速INSERT, UPDATE, DELETE语句的执行,也就是用FORALL语句来替代循环语句。
2.加速SELECT,用BULK COLLECT INTO 来替代INTO。

create table student_tmp as select sno,sname from student where 0=1;

--删除主键约束
alter table student drop constraint SYS_C0040802; --执行两遍插入
insert into student select * from student where sno=50; declare cursor cur_std(stdid student.sno%type) is select sno,sname from student where sno=stdid;
type student_table_type is table of cur_std%rowtype index by pls_integer;
student_table student_table_type; begin
open cur_std(50);
fetch cur_std bulk collect into student_table;
close cur_std; for i in 1..student_table.count loop
dbms_output.put_line(student_table(i).sno || ' ' || student_table(i).sname);
end loop; forall i in student_table.first..student_table.last
insert into student_tmp values(student_table(i).sno, student_table(i).sname);
commit; end; --清理实验环境
drop table student purge;
drop package pkg_test01;

附:动态性能表V$OPEN_CURSOR。

Oracle数据库游标的类型的更多相关文章

  1. Oracle数据库的锁类型

    Oracle数据库的锁类型 博客分类: oracle   Oracle数据库的锁类型 根据保护的对象不同,Oracle数据库锁可以分为以下几大类:DML锁(data   locks,数据锁),用于保护 ...

  2. 设置ORACLE数据库游标大小

    先用超级管理员(sys)登陆服务器: sqlplus "sys/***@orcl as sysdba" 连接到:Oracle 查看ORACLE最大游标数: SQL> show ...

  3. Oracle数据库返回字符类型-1~1的结果处理

    如果实体类中定义的字段是String类型,Oracle数据库中返回的是数字类型,那么Oracle返回0.xxx的时候会丢失前面的0. 要想不丢失0,那么数据库返回的就要是字符串类型的,所以要将返回值转 ...

  4. Oracle数据库游标,序列,存储过程,存储函数,触发器

    游标的概念:     游标是SQL的一个内存工作区,由系统或用户以变量的形式定义.游标的作用就是用于临时存储从数据库中提取的数据块.在某些情况下,需要把数据从存放在磁盘的表中调到计算机内存中进行处理, ...

  5. Oracle数据库—— 游标的创建和应用

    一.涉及内容 游标的创建与应用 二.具体操作 (一)填空题 1.PL/SQL 程序块主要包含3个部分:声明部分.(执行部分 ).异常处理部分. 2.自定义异常必须使用(RAISE )语句引发. (二) ...

  6. PHP转换oracle数据库的date类型

    今天圣诞节啊,圣诞节快乐啊! 最近遇到一个很纠结的事,就是我在plsql里面查的是这样的,很正常, 但是我用程序查出来就是这样的,啊啊啊,真是崩溃啊 但是我传数据需要上面那种格式,而且我对oracle ...

  7. Oracle数据库游标精解

    游标 定义:标识结果集中数据行的一种容器(CURSOR),游标允许应用程序对查询语句返回的行结果集中的每一行进行相同或不同的操作,而不是一次对整个结果集进行同一种操作.实际上是一种能从包括多条数据记录 ...

  8. Oracle数据库中number类型在java中的使用

    1)如果不指定number的长度,或指定长度n>18 id number not null,转换为pojo类时,为java.math.BigDecimal类型 2)如果number的长度在10 ...

  9. Oracle数据库---游标

    --查询所有员工的员工号.姓名和职位的信息.DECLARE --定义游标 CURSOR emp_cursor IS SELECT empno,ename,job FROM emp; v_empno e ...

随机推荐

  1. 总结oninput、onchange与onpropertychange事件的用法和区别 书写搜索的神奇代码

    总结oninput.onchange与onpropertychange事件的用法和区别 最近手机开发一个模糊搜索的功能组建,在网上就找到这篇文章! 前端页面开发的很多情况下都需要实时监听文本框输入,比 ...

  2. Canvas Api简介1

    canvas canvas 其实对于HTML来说很简单,只是一个标签元素而已,自己并没有行为,但却把一个绘图 API 展现给客户端 JavaScript 以使脚本能够把想绘制的东西都绘制到一块画布上, ...

  3. react-native使用react-art制作SVG动画

    想要使用SVG做一个动画,郁闷了一上午终于有了一点思路.. 其实我是看了一篇国外的文章.网址:http://browniefed.com/blog/2015/05/03/getting-react-a ...

  4. PowerDesigner中在生成的数据库脚本中用name列替换comment列作为字段描述的方法

    1 PowerDesigner中在生成的数据库脚本中用name列替换comment列作为字段描述的方法如下, 依次打开Tools -- Execute Commands -- Run Script,运 ...

  5. C语言基础知识汇总

    c语言执行步骤: 一.编辑程序 1.编写c语言的代码,保存在扩展名.c的文件中,源文件. 2.编写代码有三种方式: a.vi命令方式系统自带 b.ultraedit网络下载 c.xcode网络下载 二 ...

  6. UVALive3516Exploring Pyramids(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 题目意思:有一棵多叉树,每个结点的子节点有左右之分(即要按照顺序查找),从跟结点开 ...

  7. (原)ubuntu14手动安装matplotlib1.5

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5681059.html 参考网址: http://matplotlib.org/users/instal ...

  8. MySQL 初学笔记 ① -- MySQL用户登录权限控制

    1. MySQL 登录 MySQL -u username -p 2. MySQL 创建用户 use mysql //进入mysql 表 INSERT INTO user (Host,User,Pas ...

  9. ssh整合web.xml过滤器和监听器的配置 .

    延迟加载过滤器 Hibernate 允许对关联对象.属性进行延迟加载,但是必须保证延迟加载的操作限于同一个 Hibernate Session 范围之内进行.如果 Service 层返回一个启用了延迟 ...

  10. [c language] getopt 其参数optind 及其main(int argc, char **argv) 参数解释

    getopt被用来解析命令行选项参数.#include <unistd.h> extern char *optarg; //选项的参数指针extern int optind, //下一次调 ...