游標結果集位置

2021-04-07 07:38:12 字數 3633 閱讀 6754

游標是從資料表中提取出來的資料,以臨時表的形式存放在記憶體中,在游標中有乙個資料指標,在初始狀態下指向的是首記錄,利用fetch語句可以移動該指標,從而對游標中的資料進行各種操作,然後將操作結果寫回資料表中。

定義游標

游標作為一種資料型別,首先必須進行定義,其語法如下。

cursor 游標名 is 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,表明游標已經開啟,但沒有提取出資料。

利用mysql游標迴圈結果集

很多時候自己新增測試資料會用到mysql程式設計,其中儲存過程就非常的重要,所以在這裡寫乙個返回使用者id號用逗號拼接的例子 現在發現mysql完全可以程式設計,一門徹底的程式語言,資料型別還是強型別的,我喜歡 use test delimiter create procedure pro8 out...

游標的結果集放入臨時表

這個儲存過程是在sqlserver環境下的,通過游標讀取資料,得到結果放入臨時表中 注 如果知道如何將游標通過迴圈讀出,妹子也是剛剛接觸儲存過程的,還望賜教。將游標遍歷的結果集放入臨時表中,最後讀取臨時表 create procedure dbo cc asdeclare id varchar 10...

游標的結果集放入臨時表

這個儲存過程是在sqlserver環境下的,通過游標讀取資料,得到結果放入臨時表中,執行儲存過程就得到臨時表的內容。將游標遍歷的結果集放入臨時表中,最後讀取臨時表 create procedure dbo cc asdeclare id varchar 10 宣告游標 declare cc curs...