1、概念

游标:从字面来理解就是游动的光标。用数据库语言来描述,游标是映射在结果集中一行数据上的位置实体,有了游标,用户就可以访问结果集中的任意一行数据了。将游标放置到某行后,即可对该行数据进行操作,最常见的操作是提取当前行数据。

2、分类

2.1、静态游标:显式游标、隐式游标

2.2、动态游标:强类型(限制)、弱类型(非限制)

3、属性

3.1、%ISOPEN    判断游标是否被打开,若打开,则%ISOPEN等于TRUE;否则等于FALSE。

3.2、%FOUND    判断游标所在行是否有效。若有效,则%FOUND等于TRUE;否则等于FALSE。

3.3、%NOTFOUND   与%FOUND相似,功能相反。

3.4、%ROWCOUNT  返回到当前位置为止,游标所读取的记录行数。

4、详细说明

4.1、显式游标:

Cursor 游标名(参数) [返回值类型] Is Select语句

OPEN 游标名(参数)

FETCH 游标名(参数) INTO 变量

CLOSE 游标名(参数)

4.2、隐式游标:

select **** into **** from **** where ****

for var_name in (select ****)

loop

end loop

for var_name in (游标名(参数))

loop

end loop

4.3、动态游标

动态游标,在运行的时候才能确定游标使用的查询。分类:
1.强类型(限制)REF CURSOR,规定返回类型
2.弱类型(非限制)REF CURSOR,不规定返回类型,可以获取任何结果集。

5、举例:

5.1、静态显式游标

 set serveroutput on;
declare
r_d2tlog d2t_Log%rowtype;
cursor cs_d2tlog is select * from d2t_log;
begin
open cs_d2tlog;
loop
fetch cs_d2tlog into r_d2tlog;
exit when cs_d2tlog%notfound;
dbms_output.put_line('activeid='||r_d2tlog.activeid||';is already read counts='||cs_d2tlog%rowcount);
end loop;
close cs_d2tlog;
end;
/

 5.2、动态弱类型游标

 declare
r_d2tlog d2t_Log%rowtype;
type d2tlog is ref cursor;
t_d2tlog d2tlog;
begin
open t_d2tlog for select * from d2t_log;
loop
fetch t_d2tlog into r_d2tlog;
exit when t_d2tlog%notfound;
dbms_output.put_line('activeid='||r_d2tlog.activeid||';is already read counts='||t_d2tlog%rowcount);
end loop;
close t_d2tlog;
end;
/

5.3、动态强类型游标

 declare
l_sqlstr varchar2();
type t_d2tlog is record(
activeid number(),
writedate date
);
type c_d2tlog is ref cursor return t_d2tlog;
st_d2tlog t_d2tlog;
sc_d2tlog c_d2tlog;
begin
open sc_d2tlog for select activeid,writedate from d2t_log;
loop
fetch sc_d2tlog into st_d2tlog;
exit when sc_d2tlog%notfound;
dbms_output.put_line('activeid='||st_d2tlog.activeid||';is already read counts='||sc_d2tlog%rowcount);
end loop;
close sc_d2tlog; open sc_d2tlog for select activeid,writedate from d2t_log where activeid=;
loop
fetch sc_d2tlog into st_d2tlog;
exit when sc_d2tlog%notfound;
dbms_output.put_line('activeid='||st_d2tlog.activeid||';is already read counts='||sc_d2tlog%rowcount);
end loop;
close sc_d2tlog; /*
l_sqlstr:='select activeid,writedate from d2t_log where activeid=124531';
open sc_d2tlog for l_sqlstr;
loop
fetch sc_d2tlog into st_d2tlog;
exit when sc_d2tlog%notfound;
dbms_output.put_line('activeid='||st_d2tlog.activeid||';is already read counts='||sc_d2tlog%rowcount);
end loop;
close sc_d2tlog;
*/
end;
/

5.4、动态强类型游标

 declare
r_d2tlog d2t_log%rowtype;
type cursor1 is ref cursor;
type cursor2 is ref cursor return d2t_log%rowtype; cs_cursor2 cursor2;
begin
dbms_output.put_line('dynamic cursor define ok'); open cs_cursor2 for select * from d2t_log;
loop
fetch cs_cursor2 into r_d2tlog;
exit when cs_cursor2%notfound;
dbms_output.put_line('activeid='||r_d2tlog.activeid||';is already read counts='||cs_cursor2%rowcount);
end loop;
close cs_cursor2;
end;
/

5.5、游标定义

 declare
r_d2tlog d2t_log%rowtype;
type cursor1 is ref cursor;
type cursor2 is ref cursor return r_d2tlog;
begin
dbms_output.put_line('dynamic cursor define error');
end;
/

程序员的基础教程:菜鸟程序员

Oracle Cursor的更多相关文章

  1. Oracle cursor学习笔记

    目录 一.oracle库缓存 1.1.库缓存简介 1.2.相关概念 1.3.库缓存结构 1.4.sql执行过程简介 二.oracle cursor 2.1.cursor分类 2.2.shared cu ...

  2. Oracle Cursor的使用

    When Oracle Database executes a SQL statement, it stores the result set and processing information i ...

  3. 转 oracle cursor 游标

    转自:http://blog.csdn.net/liyong199012/article/details/8948952 游标的概念:     游标是SQL的一个内存工作区,由系统或用户以变量的形式定 ...

  4. sql:oracle, CURSOR

    CursorsYou use a cursor to fetch rows returned by a query. You retrieve the rows into the cursor usi ...

  5. Oracle Cursor用法总结

    cursor分为三种,一是直接声明为cursor变量,二是首先声明类型再声明变量,三是声明为sys_refcursor. (1)直接声明 declare cursor emp_cur  is sele ...

  6. python3.6连接oracle数据库

    下载cx_Oracle模块模块: https://pypi.python.org/pypi/cx_Oracle/5.2.1#downloads 这里下载的是源码进行安装 [root@oracle or ...

  7. ORACLE中使用DBMS_SQL获取动态SQL执行结果中的列名和值

    1.获取动态SQL中的列名及类型 DECLARE l_curid INTEGER; l_cnt NUMBER; l_desctab dbms_sql.desc_tab; l_sqltext ); BE ...

  8. Populating Tabular Data Block Manually Using Cursor in Oracle Forms

    Suppose you want to populate a non-database data block with records manually in Oracle forms. This t ...

  9. Oracle 游标Cursor 的基本用法

    查询 SELECT语句用于从数据库中查询数据,当在PL/SQL中使用SELECT语句时,要与INTO子句一起使用,查询的 返回值被赋予INTO子句中的变量,变量的声明是在DELCARE中.SELECT ...

随机推荐

  1. WEBPACK简介

    Webpack 是一个强力的模块打包器. 所谓 包 (bundle) 就是一个 JavaScript 文件,它把一堆 资源 (assets) 合并在一起,以便它们可以在同一个文件请求中发回给客户端. ...

  2. rsync 通过 ssh 上传文件

    rsync -avzP /www/ -e ssh root@192.168.1.200:/www/ 批处理上传: #!/bin/bash for (( ; ; )) do rsync -avzP /w ...

  3. mysql 复制表结构、表数据的方法

    From: http://blog.163.com/yaoyingying681@126/blog/static/109463675201191173221759/ MySQL 添加列,修改列,删除列 ...

  4. JUC.Lock(锁机制)学习笔记[附详细源码解析]

    锁机制学习笔记 目录: CAS的意义 锁的一些基本原理 ReentrantLock的相关代码结构 两个重要的状态 I.AQS的state(int类型,32位) II.Node的waitStatus 获 ...

  5. c++程序设计之编程思想

    代码块愈小,代码的功能就愈容易管理,代码的处理和移动就愈轻松. 任何一个傻瓜都能写出计算机可以理解的代码,唯有写出人类容易理解的代码,才是优秀的程序员. 绝大多数情况下,函数应该放在它所使用的数据的所 ...

  6. 关于C#的委托(delegate)的自我理解

    首先描述一个事情,一个老师饿了,他要去买东西填饱肚子,然后他发现他的学生“小李”在玩,没学习,于是就委托“小李”去帮他买吃的. 根据这件事我们来分析: 首先得有个老师(老师饿了是他的方法,老师买东西也 ...

  7. spring IOC核心原理

    下面来了解一下Spring到底是怎么运行的. public static void main(String[] args) { ApplicationContext context = new Fil ...

  8. Java7的垃圾回收

    HotSpot JVM一共有4个垃圾回收器:Serial(串行).Parallel / Throughput(并行).CMS(并发).and the new kid on the block G1(G ...

  9. nginx 自动忽略request中header name包含下划线参数的解决方法

    使用nginx过程中遇到了个问题,就是request中的header name中如果包含下划线会自动忽略掉,导致服务器接收不到该字段的内容,以下为解决方法: nginx默认request的header ...

  10. oracle 触发器学习

    触发器使用教程和命名规范 目  录触发器使用教程和命名规范 11,触发器简介 12,触发器示例 23,触发器语法和功能 34,例一:行级触发器之一 45,例二:行级触发器之二 46,例三:INSTEA ...