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

首頁 > 數據庫 > Oracle > 正文

Oracle 9i 游標

2024-08-29 13:45:24
字體:
來源:轉載
供稿:網友
  游標是從數據表中提取出來的數據,以臨時表的形式存放在內存中,在游標中有一個數據指針,在初始狀態下指向的是首記錄,利用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。
Oracle 9i 游標
打開游標

    要使用創建好的游標,接下來要打開游標,語法結構如下:
    open 游標名;
    打開游標的過程有以下兩個步驟:
    (1)將符合條件的記錄送入內存。
    (2)將指針指向第一條記錄。

提取游標數據

    要提取游標中的數據,使用fetch命令,語法形式如下。
    fetch 游標名 into 變量名1, 變量名2,……;
    或
    fetch 游標名 into 記錄型變量名;
    在【SQLPlus Worksheet】中執行下列PL/SQL程序,該程序定義cursorrecord變量是游標mycursor的記錄行變量,在游標mycursor的結果中找到sal字段大于800的第一個記錄,顯示deptno字段的內容。
    執行結果如圖9.36所示。
Oracle 9i 游標
    ―――――――――――――――――――――――――――――――――――――
    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。
Oracle 9i 游標
    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。

Oracle 9i 游標
    3. %notfound屬性
    該屬性是%found屬性的反邏輯,常被用于退出循環。
    在【SQLPlus Worksheet】中執行下列PL/SQL程序。該程序利用%notfound屬性判定游標是否沒有數據。
    執行結果如圖9.39所示。

(圖片較大,請拉動滾動條觀看)
    【配套程序位置】:第9章/ notfoundattribute.sql。
Oracle 9i 游標
    4. %rowcount屬性
    該屬性用于返回游標的數據行數。
    在SQLPlus Worksheet的【代碼編輯區】執行下列PL/SQL程序,該程序利用%rowcount屬性判定游標數據行數。
    執行結果如圖9.40所示。

(圖片較大,請拉動滾動條觀看)
    【配套程序位置】:第9章/ rowcountattribute.sql。
    若返回值為0,表明游標已經打開,但沒有提取出數據。
Oracle 9i 游標

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东乡族自治县| 旬阳县| 新巴尔虎左旗| 遂昌县| 渭南市| 新巴尔虎右旗| 堆龙德庆县| 高陵县| 东至县| 九龙县| 榆中县| 蓝田县| 烟台市| 龙口市| 荔浦县| 共和县| 台湾省| 咸丰县| 固原市| 安陆市| 隆回县| 靖宇县| 库车县| 泽州县| 普洱| 娄烦县| 夏津县| 榆林市| 通许县| 武冈市| 临邑县| 连城县| 鸡泽县| 修文县| 沅陵县| 鲁甸县| 馆陶县| 织金县| 宝丰县| 朝阳市| 凤冈县|