游标结果集位置

游标是从数据表中提取出来的数据,以临时表的形式存放在内存中,在游标中有一个数据指针,在初始状态下指向的是首记录,利用fetch语句可以移动该指针,从而对游标中的数据进行各种操作,然后将操作结果写回数据表中。

定义游标

游标作为一种数据类型,首先必须进行定义,其语法如下。
cursor 游标名 is select 语句;
cursor是定义游标的关键词,select是建立游标的数据表查询命令。
以scott用户连接数据库,在【SQLPlus Worksheet】中执行下列PL/SQL程序,该程序定义tempsal为与scott.emps数据表中的sal字段类型相同的变量,mycursor为从scott.emp数据表中提取的sal大于tempsal的数据构成的游标。
执行结果如图9.35所示。
―――――――――――――――――――――――――――――――――――――
set serveroutput on
declare
tempsal scott.emp.sal%type;
cursor mycursor is
select * from scott.emp
where sal>tempsal;
begin
tempsal:=800;
open mycursor;
end;
―――――――――――――――――――――――――――――――――――――
【配套程序位置】:第9章/ cursordefine.sql。

打开游标

要使用创建好的游标,接下来要打开游标,语法结构如下:
open 游标名;
打开游标的过程有以下两个步骤:
(1)将符合条件的记录送入内存。
(2)将指针指向第一条记录。


提取游标数据

要提取游标中的数据,使用fetch命令,语法形式如下。
fetch 游标名 into 变量名1, 变量名2,……;

fetch 游标名 into 记录型变量名;
在【SQLPlus Worksheet】中执行下列PL/SQL程序,该程序定义cursorrecord变量是游标mycursor的记录行变量,在游标mycursor的结果中找到sal字段大于800的第一个记录,显示deptno字段的内容。
执行结果如图9.36所示。

―――――――――――――――――――――――――――――――――――――
set serveroutput on
declare
tempsal scott.emp.sal%type;
cursor mycursor is
select * from scott.emp
where sal>tempsal;
cursorrecord mycursor%rowtype;
begin
tempsal:=800;
open mycursor;
fetch mycursor into cursorrecord;
dbms_output.put_line(to_char(cursorrecord.deptno));
end;
―――――――――――――――――――――――――――――――――――――
【配套程序位置】:第9章/ cursorfetch.sql。

关闭游标

使用完游标后,要关闭游标,使用close命令,语法形式如下:
close 游标名;

游标的属性

游标提供的一些属性可以帮助编写PL/SQL程序,游标属性的使用方法为:游标名[属性],例如mycursor%isopen,主要的游标属性如下。
1. %isopen属性
该属性功能是测试游标是否打开,如果没有打开游标就使用fetch语句将提示错误。
在【SQLPlus Worksheet】中执行下列PL/SQL程序,该程序利用%isopen属性判断游标是否打开。执行结果如图9.37所示。
―――――――――――――――――――――――――――――――――――――
set serveroutput on
declare
 tempsal scott.emp.sal%type;
 cursor mycursor is
 select * from scott.emp
 where sal>tempsal;
 cursorrecord mycursor%rowtype;
 begin
 tempsal:=800;
 if mycursor%isopen then
 fetch mycursor into cursorrecord;
 dbms_output.put_line(to_char(cursorrecord.deptno));
 else
 dbms_output.put_line('游标没有打开!');
 end if;
end;
―――――――――――――――――――――――――――――――――――――
【配套程序位置】:第9章/ isopenattribute.sql。

2. %found属性
该属性功能是测试前一个fetch语句是否有值,有值将返回true,否则为false。
在【SQLPlus Worksheet】中执行下列PL/SQL程序。该程序利用%found属性判断游标是否有数据。
执行结果如图9.38所示。
―――――――――――――――――――――――――――――――――――――
set serveroutput on
declare
tempsal scott.emp.sal%type;
cursor mycursor is
 select * from scott.emp
 where sal>tempsal;
cursorrecord mycursor%rowtype;
begin
tempsal:=800;
open mycursor;
fetch mycursor into cursorrecord;
if mycursor%found then
dbms_output.put_line(to_char(cursorrecord.deptno));
else
dbms_output.put_line('没有数据!');
end if;
end;
―――――――――――――――――――――――――――――――――――――
【配套程序位置】:第9章/ foundattribute.sql。

3. %notfound属性
该属性是%found属性的反逻辑,常被用于退出循环。
在【SQLPlus Worksheet】中执行下列PL/SQL程序。该程序利用%notfound属性判断游标是否没有数据。
执行结果如图9.39所示。

【配套程序位置】:第9章/ notfoundattribute.sql。

4. %rowcount属性
该属性用于返回游标的数据行数。
在SQLPlus Worksheet的【代码编辑区】执行下列PL/SQL程序,该程序利用%rowcount属性判断游标数据行数。
执行结果如图9.40所示。

【配套程序位置】:第9章/ rowcountattribute.sql。
若返回值为0,表明游标已经打开,但没有提取出数据。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值