------------------------------------无参数游标-------------------------------
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;
|
相关推荐
Oracle存储过程、游标、函数的详解
详细的oracle游标用法,简单易懂,是学习游标的好材料
oracle游标 详解 精析 示例 真正能把游标讲透、说全、调理清晰的讲义。 游标犹如C语言的指针:灵活、实用、高效。 游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。 游标是一个通过定义...
Oracle存储过程游标详解,针对Oracle存储过程游标详细说明。
-- 隐式游标 (使用的表为Oracle默认自带的emp表) -- sql%rowcount:影响记录条数 sql%found:是否有满足条件的记录 set serveroutput on; declare v_ename a_emp.ename%type; begin select ename into v_ename ...
主要给大家介绍了关于Oracle游标使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或着工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Oracle中的游标和函数详解 1.游标 游标是一种 PL/SQL 控制结构;可以对 SQL 语句的处理进行显示控制,便于对表的行数据 逐条进行处理。 游标并不是一个数据库对象,只是存留在内存中。 操作步骤: 声明游标 ...
主要介绍了详解Oracle游标的简易用法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
ORACLE 游标的介绍.详解
存储过程和游标详解.希望能帮助大你们,很好!
oracle经典安装与存储过程游标及oracle优化
Oracle存储过程返回游标有两种实现方法一种是声明系统游标,一种是声明自定义游标,本文将详细介绍,需要了解的朋友可以参考下
oracle游标的使用的代码和详解过程的描述
第一章 PL/SQL程序设计简介 第二章 PL/SQL块结构和组成元素 ...第四章 游标的使用 第五章 异常错误处理 第六章 存储过程和函数 第七章 包的创建和应用 第八章 触发器 第九章 ORACLE提供的常用包
文档目录内容如下: Oracle PL/SQL语言初级教程 1 目录 1 1.PL/SQL语言基础 3 过程 5 包(package) 6 ...在游标FOR循环中使用查询 86 游标中的子查询 86 9.PL/SQL异常处理初步 88 异常传播 91 常用异常处理方法 93