标题: Oracle 9i 游标
问天
元帅
Rank: 1


元帅勋章 终身成就勋章
UID 11493
精华 187
积分 34199
帖子 33353
威望 91
金币 13148
热心 2617
阅读权限 100
注册 2006-4-7
状态 在线
Oracle 9i 游标

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


网友 问天 签名 - 网友社区 请您回个帖。谢谢
PR查询 免费域名 免费空间
顶部
[广告] 免费域名(Free Subdomain) 免费空间(Free hosting) PR查询(Google Pagerank)



当前时区 GMT+8, 现在时间是 2008-10-7 09:57
信产部ICP备案:京ICP备05066424号 北京市公安局网监备案:1101050648号

Powered by Discuz! 5.5.0
清除 Cookies - 联系我们 - 网友俱乐部 - Archiver - WAP