Oracle 9i 游标

王朝oracle·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

游标是从数据表中提取出来的数据,以临时表的形式存放在内存中,在游标中有一个数据指针,在初始状态下指向的是首记录,利用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,表明游标已经打开,但没有提取出数据。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航