------------------------------------无参数游标-------------------------------
declare
snamevarchar2(20);--声明变量
cursorstudent_cursorisselectsnamefromstudent;--声明游标
begin
openstudent_cursor;--打开游标
fetchstudent_cursorintosname;--让游标指针往下移动
whilestudent_cursor%found--判断游标指针是否指向某行记录
loop--遍历
dbms_output.put_line('学生姓名'||sname);
fetchstudent_cursorintosname;
endloop;
closestudent_cursor;
end;
------------------------------------有参数游标-------------------------------
declare
sname student.sname%type;
sno student.sno%type;
cursorstudent_cursor(input_snonumber)isselects.sname,s.snofromstudent
swheres.sno>input_sno;--声明带参数的游标
begin
sno:=&请输入学号;--要求从客户端输入参数值,"&"相当于占位符;
openstudent_cursor(sno);--打开游标,并且传递参数
fetchstudent_cursorintosname,sno;--移动游标
whilestudent_cursor%found
loop
dbms_output.put_line('学号为:'||sno||'姓名为:'||sname);
fetchstudent_cursorintosname,sno;
endloop;
closestudent_cursor;
end;
------------------------------------循环游标-------------------------------
-- Created on 18-1月-15 by 永文
declare
stu1 student%rowtype;--这里也不需要定义变量来接收fetch到的值
cursorstudent_cursorisselect*fromstudent;
begin
openstudent_cursor;--这里不需要开启游标
forstu1instudent_cursor
loop
dbms_output.put_line('学生学号:'||stu1.sno||'学生姓名:'||stu1.sname);
fetchstudent_cursorintostu1;--也不需要fetch了
endloop;
closestudent_cursor;--这里也不需要关闭游标
end;
------------------------------------使用游标更新行-------------------------------
declare
stu1 student%rowtype;
cursorstudent_cursorisselect*fromstudent swheres.snoin(2,3)forupdate;--创建更新游标
begin
openstudent_cursor;
fetchstudent_cursorintostu1;--移动游标
whilestudent_cursor%found--遍历游标,判断是否指向某个值
loop
updatestudentsetsage=sage+10wherecurrentofstudent_cursor;--通过游标中的信息更新数据
fetchstudent_cursorintostu1;--移动游标
endloop;
closestudent_cursor;
end;
declare
stu1 student%rowtype;
cursorstudent_cursorisselect*fromstudent swheres.snoin(2,3)forupdate;--创建更新游标
begin
openstudent_cursor;
-- fetch student_cursor into stu1;--移动游标
-- while student_cursor%found--遍历游标,判断是否指向某个值
loop
fetchstudent_cursorintostu1;--移动游标
exitwhenstudent_cursor%notfound;
updatestudentsetsage=sage+10wherecurrentofstudent_cursor;--通过游标中的信息更新数据
endloop;
closestudent_cursor;
end;
------------------------------------使用fetch ... bulk collect into-------------------------------
declare
cursor my_cursorisselectenamefromempwheredeptno=10;--声明游标
type ename_table_typeistableofvarchar2(10);--定义一种表类型,表中的属性列为varchar2类型
ename_table ename_table_type;--通过上面定义的类型来定义变量
begin
open my_cursor;--打开游标
fetchmy_cursorbulkcollectinto ename_table;--移动游标
for iin1..ename_table.countloop
dbms_output.put_line(ename_table(i));
endloop;
closemy_cursor;
end;
-----------------------------------显示游标题目--------------------------------------
SQL>select*fromstudent;
XH XM
---------- ----------
1A
2B
3C
4D
SQL>select*fromaddress;
XH ZZ
---------- ----------
2郑州
1开封
3洛阳
4新乡
完成的任务:给表student添加一列zz,是varchar2(10)类型;
再从address中,将zz字段的数值取出来,对应的插入到
student新增的zz列中。
即:得到的结果:student表中,是:
XH XM ZZ
-- ---------- ------
1A 开封
2B 郑州
3C 洛阳
4D 新乡
declare
stu1 student%rowtype;
add1 address%rowtype;
cursorstudent_cursorisselect*fromstudentforupdate;--声明更新游标
cursoraddress_cursorisselect*fromaddress;--声明游标
begin
openstudent_cursor;--打开游标
fetchstudent_cursorintostu1;--移动游标
whilestudent_cursor%found--判断游标是否指向某条记录
loop
openaddress_cursor;--打开另外一个游标
fetchaddress_cursorintoadd1;--移动游标
whileaddress_cursor%found--判断游标是否指向某条记录
loop
ifadd1.xh=stu1.xhthen--判断两个游标所指向的记录中xh的值是否相等
updatestudent ssets.zz=add1.zzwherecurrentofstudent_cursor;--假如相等就更新游标所指向的记录值
endif;
fetchaddress_cursorintoadd1;--移动游标
endloop;
closeaddress_cursor;--关闭游标
fetchstudent_cursorintostu1;--移动游标
endloop;
closestudent_cursor;--关闭游标
end;
|
相关推荐
使用游标更新数据库 使用游标更新数据库是指在游标定位下,修改或删除表中指定的数据行。使用游标对数据进行更新可以提高数据库的性能和效率。 1. 游标更新的基本概念 游标是数据库中的一种数据结构,它可以用来...
游标图例用来显示图形中的游标,如图1所示。在图形上用游标可读取绘图区域上某个点的确切值,游标值会显示在游标图例中。 图1 图形缩放方式 图2 游标图例 选择游标图例右键快捷菜单的“创建游标”,在...
SQL Server对游标的使用要遵循:声明游标–打开游标–读取数据–关闭游标–删除游标。下面让我们来看看几种常用游标是怎么使用的! 1、只读游标的使用(只能使用next提取数据) --声明一个只读游标 declare cur_stu ...
PRINT '游标中的第-2行[相对地址]:' + @value; -- 填充数据. FETCH PRIOR FROM c_test_main INTO @id, @value; PRINT '游标中的上一行:' + @value; -- 填充数据. FETCH NEXT FROM c_test_main INTO @id, @...
设置波形图的游标,设置游标参数,设置多个游标,可自由拖动。
Mysql游标(循环操作)
机械制图:仪表与量具-游标卡尺的使用 机械制图中,仪表与量具是非常重要的组成部分,而游标卡尺是其中一种常用的测量工具。游标卡尺的使用规则、分类、测量原理和读数方法等都是机械制图中需要掌握的知识点。 ...
详细介绍游标的创建,使用。里面有详细的例子,是见过的所有学习游标和过程最好的接招
SQL Server遍历表一般都要用到游标,SQL Server中可以很容易的用游标实现循环,实现SQL Server遍历表中记录。本文将介绍利用使用表变量和游标实现数据库中表的遍历。 表变量来实现表的遍历 以下代码中,代码块之间的...
游标算法_伪码.pdf
学习之用,游标操作多个数据库 学习之用,游标操作多个数据库 学习之用,游标操作多个数据库
游标可以分为隐式游标和显式游标两种,隐式游标由 PL/SQL 管理,隐式游标打开时查询开始,查询结束时隐式游标自动关闭。显式游标需要在 PL/SQL 块的声明部分声明,在执行部分或异常处理部分打开,取出数据,关闭。 ...
jdbcTemplate分页彻底解决,使用游标滚动jdbcTemplate分页彻底解决,使用游标滚动
什么是游标?游标有什么作用?Oracle游标的类型?Oracle游标的状态有哪些,怎么使用游标属性?如何使用显示游标,?如何遍历循环游标?
学习游标有一段时间了,也用过几次游标来解决问题,但是本次的应用让我更加深入的了解到游标的神奇!我写的这个程序是用在ERP系统中将库存分配到当天要出货的订单上,由于有订单不一定有库存,有库存的时候,当天不...
oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结
海拉传感器游标算法
DB2游标及动态SQL,异常处理,sqlcode,sqlstate
游标是SQL的一个内存工作区,由系统或用户以变量的形式定义。游标的作用就是用于临时存储从数据库中提取的数据块。在某些情况下,需要把数据从存放在磁盘的表中调到计算机内存中进行处理,最后将处理结果显示出来或...
资源为多态VI(适配数值数组和波形),需要作为子VI进行调用,调用时在父类VI中将波形图的引用创建好并连接至子VI输入端,同时波形图的游标应提前创建好。由于文件路径变化下载后如提示找不到子vi请手动指定路径。