1、概念

    游标是指向SQL处理的内存区的句柄或指针。当使用一个PL/SQL块来执行DML语句或只返回一行结果的SELECT语句时,系统将自动创建一个隐式游标。如果SQL语句返回多个结果,就必须创建一个显示游标

--游标的属性
--(1)cur_name%rowcount :指出处理的行数
-- (2) cur_name%found :处理了一行或多行返回TRUE否则FALSE 如 WHILE CUR%FOUND中
--(3)cur_name%notfound :如果没有处理行返回TRUE,否则FALSE 如 EXIT WHEN CUR%NOTFOUND
--(4)cur_name%isopen :如果处理之后不关闭游标,则为TRUE,关闭后为FALSE。发生在隐式游标中时
-- 总是为FALSE;
2、隐式游标例程
declare
tname student.name%type;
tage student.age%type;
begin
select name,age into tname,tage from student where id = 'S001';
--返回零行货多行,执行报错
dbms_output.put_line('name= '||tname||'age= '||tname);
end

3、显示游标

3.1、定义游标

--cursor cursor_name is select _statement;
cursor c_stu is select from student; c_stu sys_refcursor;--系统游标
--定义游标(3)
--先在包中定义游标,及用于检索的结构体,
--这里的结构体相当于 游标%rowtype
create or replace package p_stu
as
type c_su is ref cursor;
type rc_stu is record(
name student.name%type,
age student.age%type
);
end;
--使用包中的游标
declare
c_student p_stu.c_stu;
crw_stu p_stu.rc_stu;
begin
open c_student for select name,age from student;
loop
fetch c_student into crw_stu;
exit when c_student%notfound;
dbms_output.put_line('name= '||crw_stu.name||' age= '||crw_stu.age);
end loop;
end;

游标使用案例

declare
--定义一个游标
cursor c_stu is select name,age from student;
--定义一个游标变量
cw_stu c_stu%rowtype;
begin
for cw_stu in c_stu loop
dbms_output.put_line('姓名='||cw_stu.name||' 年龄='||cw_stu.age);
end loop;
end
--使用 fetch 必须明确打开和关闭游标
declare
cursor c_stu is select name,age from student;
--定义一个游标变量
cw_stu c_stu%rowtype;
begin
open c_stu;
loop
fetch c_stu into cw_stu;
exit when c_stu%notfound;
dbms_output.put_line('姓名='||cw_stu.name||' 年龄='||cw_stu.age);
end loop
close c_stu;
end
--使用 while fetch 遍历数据  %found属性
declare
cursor c_stu is
select name,age from student;
cw_stu c_stu%rowtype;
begin
open c_stu;
fetch c_stu into cw_stu;
while c_stu%found loop
dbms_output.put_line('姓名='||cw_stu.name||' 年龄='||cw_stu.age);
end loop
close c_stu;
end

3.3、在存储过程中返回游标

--  注意:在declare块中使用存储过程返回的游标时:
-- (1)不能定义系统游标变量,编译错误。如:cw_Stu C_Stu%rowtype;
-- (2)可以使用结构体变量,但不能使用for循环如:for rw_stu in c_stu loop
-- 将提示 c_stu '不是过程或尚未定义'。
-- (3)游标不可显示打开或关闭,如 open c_stu;表达式类型错误。
create or replace procedure (
CID in varchar2,
Cur_Stu out sys_refcursor--不能定义系统变量
)as
begin
open Cur_Stu for select name,age from student where ClassId=Id;
end ;
--  使用
-- 测试结果:
-- (1)不能定义系统游标变量,编译错误。如:cw_Stu C_Stu%rowtype;
-- (2)可以使用结构体变量,但不能使用for循环如:for rw_stu in c_stu loop
-- 将提示 c_stu '不是过程或尚未定义'。
-- (3)游标不可显示打开或关闭,如 open c_stu;表达式类型错误。
declare
C_Stu Sys_RefCursor;
type rec_stu is record(
tname student.name%type;
tage student.age%type
);
rw_stu rec_stu;
begin
pro_syscur('C001',c_stu);
loop
--也可以写成 fetch c_stu into rw_stu.tname,rw_stu.tage;
--或直接定义表字段类型变量
fetch c_stu into rw_stu;
exit when c_stu%notfound;
dbms_output.put_line('姓名='||rw_stu.tname||' 年龄='||rw_stu.tage);
end loop;
end

(1)返回自定义游标

--第一步,在包中定义游标,及用于遍历的结构体
create or replace package pack_stu
as
type c_pubsur is ref cursor;
type re_stu is record(
tname student.name%type,
tage student.age%type
);
num number;
end --第二步,将存储过程的返回类型设为上面包中游标类型
create or replace procedure p_stu(
cid in varchar2,c_s out pack_stu.c_pubsur
)as
begin
open c_s for select name,age from student where classid=cid;
--第三步,使用游标,注意事项与系统游标一致。
end declare
c_stu pack_stu.c_pubsur;
cw_stu pack_stu.re_stu;
begin
p_stu('D001',c_stu);
loop
fetch c_stu into cw_stu;
exit when c_stu%notfound;
dbms_output.put_line('姓名='||cw_stu.tname||' 年龄='||cw_stu.tage);
end loop
end

Oracle游标整理二的更多相关文章

  1. oracle 游标使用大全

    转:http://www.cnblogs.com/fjfzhkb/archive/2007/09/12/891031.html oracle的游标和例子! 游标-----内存中的一块区域,存放的是se ...

  2. Oracle游标的使用示例

    此文是使用Oracle游标的几种方式,for...in会自动打开游标,fetch...into需要手动打开游标,游标类似于一个只会往前移动的指针,每次指向数据集中的一行数据,通过游标可以打开数据集,也 ...

  3. Oracle游标解析

    本节对Oracle中的游标进行详细讲解. 本节所举实例来源Oracle中scott用户下的emp表dept表: 一.游标: 1.概念: 游标的本质是一个结果集resultset,主要用来临时存储从数据 ...

  4. oracle游标的定义使用

    oracle游标的定义使用 2008-02-23 15:12:57|  分类: oracle|字号 订阅 游标中定义的参数只要定义类型,不用定义长度,精度等: 游标使用一: declarecursor ...

  5. Oracle 游标使用总结(好文章)

    游标(CURSOR)也叫光标,在关系数据库中经常使用,在PL/SQL程序中可以用CURSOR与SELECT一起对表或者视图中的数据进行查询并逐行读取. Oracle游标分为显示游标和隐式游标. 显示游 ...

  6. Oracle 游标示例,带异常处理

    Oracle游标示例一则,带异常处理. DECLARE CURSOR c_dl IS SELECT ID, NSRSBH, WSPZXH, ZXYY_DM, HZRQ, SWJG_DM, GXSJ F ...

  7. Oracle游标带参数

    Oracle游标是可以带参数的,而SqlServer的游标就不可以了 create or replace procedure a as cursor b(c_id int)is select * fr ...

  8. Oracle 游标使用(转)

    这个文档几乎包含了oracle游标使用的方方面面,全部通过了测试 ; ; dbms_output.put_line(sql) loop dbms_output.put_line( ; ; ; r_te ...

  9. Oracle 数据库整理表碎片

    Oracle 数据库整理表碎片 转载:http://kyle.xlau.org/posts/table-fragmentation.html 表碎片的来源 当针对一个表的删除操作很多时,表会产生大量碎 ...

随机推荐

  1. Qt 之 去除窗口部件被选中后的焦点虚线框(设置Qt::NoFocus即可)

    http://blog.csdn.net/goforwardtostep/article/details/53420529

  2. php日期时间函数 整理

    设定系统默认时区 date_default_timezone_get() $tz='America/Los_Angeles'; 返回系统默认时区 date_default_timezone_set($ ...

  3. jQuery 遍历json数组的实现代码

    <script type="text/javascript"> "}]; $(d1).each(function(){ alert(this.text+&qu ...

  4. 如何解决jQuery Validation针对动态添加的表单无法工作的问题?

    为了充分利用ASP.NET MVC在服务端呈现HTML的能力,在<利用动态注入HTML的方式来设计复杂页面>一文中介绍了,通过Ajax调用获取HTML来呈现复杂页面中某一部分界面的解决方案 ...

  5. TermServDevices错误

    来源TermServDevices的错误日志,日志内容 事类型:     错误 事件来源:     TermServDevices 描述 打印机 XXXX 所需的驱动程序 XXX 未知.登录之前,请与 ...

  6. 删除Checkout with Multiple Addresses

    如果选择No,您的客户在结账时只可以输入一个收货地址,订单的货物都会送到这个地址.如果您选择Yes,您的客户将可以选择发货到多个地址,在购物车中的Proceed to Checkout按钮下面将会出现 ...

  7. action script 3如何检测播放器域

    检测播放器域   用户在上面观看媒体内容的网页的 URL 和域并非始终随时可用.如果托管网站允许,您可使用 ExternalInterface 类获取确切 URL.尽管如此,允许第三方视频播放器的一些 ...

  8. dubbo的简单使用

    整个过程大致是这样的 1.注册中心使用zookeeper,地址为192.168.192.128:2181! 2.首先服务方 所在的服务器是127.0.0.1:8081 服务方提供的接口: public ...

  9. glOrtho、glFrustum && glPerspective

    glOrtho         :正交投影,摄像机可以位于裁剪体内,所以near和far可以取两个正值或者一正一负 glFrustum     :透视投影,摄像机不可以位于裁剪体内,所以near和fa ...

  10. eclipse的快捷键大全

    Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...