国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > Oracle > 正文

Oracle 9i 游標

2024-08-29 13:32:08
字體:
來源:轉載
供稿:網友
  游標是從數據表中提取出來的數據,以臨時表的形式存放在內存中,在游標中有一個數據指針,在初始狀態下指向的是首記錄,利用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,表明游標已經打開,但沒有提取出數據。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南投市| 湖北省| 修水县| 通州市| 金湖县| 长宁区| 保德县| 沅江市| 定州市| 洛浦县| 仪征市| 莱州市| 梅州市| 扎兰屯市| 寿光市| 乌审旗| 隆化县| 旬阳县| 瑞安市| 临洮县| 伊金霍洛旗| 阜宁县| 大田县| 佛坪县| 丹棱县| 鸡东县| 东兰县| 延庆县| 辽阳市| 津南区| 贵州省| 中宁县| 万盛区| 孟州市| 茶陵县| 潜山县| 正蓝旗| 德化县| 茂名市| 凌云县| 枣阳市|