Oracle Cursor
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的更多相关文章
- Oracle cursor学习笔记
目录 一.oracle库缓存 1.1.库缓存简介 1.2.相关概念 1.3.库缓存结构 1.4.sql执行过程简介 二.oracle cursor 2.1.cursor分类 2.2.shared cu ...
- Oracle Cursor的使用
When Oracle Database executes a SQL statement, it stores the result set and processing information i ...
- 转 oracle cursor 游标
转自:http://blog.csdn.net/liyong199012/article/details/8948952 游标的概念: 游标是SQL的一个内存工作区,由系统或用户以变量的形式定 ...
- sql:oracle, CURSOR
CursorsYou use a cursor to fetch rows returned by a query. You retrieve the rows into the cursor usi ...
- Oracle Cursor用法总结
cursor分为三种,一是直接声明为cursor变量,二是首先声明类型再声明变量,三是声明为sys_refcursor. (1)直接声明 declare cursor emp_cur is sele ...
- python3.6连接oracle数据库
下载cx_Oracle模块模块: https://pypi.python.org/pypi/cx_Oracle/5.2.1#downloads 这里下载的是源码进行安装 [root@oracle or ...
- ORACLE中使用DBMS_SQL获取动态SQL执行结果中的列名和值
1.获取动态SQL中的列名及类型 DECLARE l_curid INTEGER; l_cnt NUMBER; l_desctab dbms_sql.desc_tab; l_sqltext ); BE ...
- 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 ...
- Oracle 游标Cursor 的基本用法
查询 SELECT语句用于从数据库中查询数据,当在PL/SQL中使用SELECT语句时,要与INTO子句一起使用,查询的 返回值被赋予INTO子句中的变量,变量的声明是在DELCARE中.SELECT ...
随机推荐
- ps应用
1.选中图层 ctrl+鼠标左键(win) command+鼠标左键(mac) 2.初始化 右侧:图层,历史记录,信息(面板选项-rgb,文档尺寸,像素),字符 编辑-首选项-单位与标尺-像素 窗口- ...
- linux expect命令使用入门
expect的核心是spawn expect send set spawn:spawn是进入expect环境后才可以执行的expect内部命令,相当于shell中的内置命令,通过它,调用需要执行的 ...
- Ninject之旅之十:Ninject自定义提供者
摘要 提供者是特殊的工厂类,Ninject使用它来实例化解析类型.任何时候我们绑定一个服务类型到一个组件,我们都隐式地关联那个服务类型到一个可以实例化那个组件的提供者.这个隐藏的提供者被称为Stand ...
- 关于C中struct和union长度的详解
这几天看<代码大全>中的第十三章---不常见的数据类型,里面讲解到了C语言中的struct以及对指针的解释,联想到以前看过相关的关于C语言中stuct长度的文章,只是现在有些淡忘了,因此今 ...
- 将对象的所有属性名放到一个数组中 || 获得对象的所有属性名 || return;不具有原子性 || 怎样自己制作异常|| 判断对象有没有某个属性 || 当传递的参数比需要的参数少的时候,没有的值会被赋予undefined || 获得函数实际传递的参数 || 怎么用函数处理一个对象 || 用一个名字空间定义一个模块所有的函数 || 给一个对象添加方法
获得对象的所有属性名 || 将对象o的所有属性名放到数组中 var o = {x:1,y:2,z:3}; var arr = []; var i = 0; for(arr[i++] in o){};/ ...
- An exception occurred while acquiring a poolable resource. Will retry.
我的是mysql-connector-java-5.1.20.jar加到lib下面就行了.或者在pom中加入配置也行.
- javascript算法
代码运行环境: nodejs + mochajs /* *选择排序 *每次查找数组最小数据 *将最小数据排到左侧 */ var assert = require('assert'); describe ...
- DataSnap修改数据ApplyUpdates出现错误:连接繁忙导致另一个命令
最近准备尝试用DBExpress做个SQL Serer应用,在学习的时候发现一个问题使用DBExpress连接Sql server 2008 express使用以下控件SQLConnection-&g ...
- vim 大全用法
vim中常用设置和操作: 在Linux系统下: 打开vi 文件: 0 数字0,跳转至行首 ^ 跳转至行第一个非空字符 $ 跳转至行尾 vim 括号匹配跳转操作: ctrl+] 跳转至函数或 ...
- CSS3新增属性
1>RGBA透明度(红.绿.蓝.alpha透明度) 2>块阴影 box-shadow(标签).text-shadow(文字) 3>圆角阴影 border-radius 4>边框 ...