摘要:詳細介紹Oracle資料庫中關於遊標的定義和使用。通過執行個體操作來深入瞭解cursor的用法和用處。
一:相關概念 1、concept
When Oracle Database executes aSQL statement , it stores the result set and processing information in anunnamed private SQL area . A pointer to this unnamed area , called a cursor ,let you retrieve the rows of the result set one at a time . Cursor attributesreturn information about the state of the cursor .
2、概念:
遊標是SQL的一個記憶體工作區,由系統或使用者以變數的形式定義。遊標的作用就是用於臨時儲存從資料庫中提取的資料區塊。在某些情況下,需要把資料從存放在磁碟的表中調到電腦記憶體中進行處理,最後將處理結果顯示出來或最終寫回資料庫。這樣資料處理的速度才會提高,否則頻繁的磁碟資料交換會降低效率。Cursor類型:靜態資料指標——分為顯式(explicit)遊標和隱式(implicit)遊標、REF遊標——動態資料指標、是一種參考型別、類似於指標。
二:具體類型及使用 1、implicit cursor
1) explanation:Everytime you run either a SQL DML statement or a PL/SQLSELECTINTO statement, PL/SQLopens an implicit cursor. You can get information about this cursor from itsattributes, but you cannot control it. After the statement runs, the databasecloses the cursor; however, its attribute values remain available until anotherDML orSELECTINTO statement runs.
2) implicit cursor(隱式遊標)由系統自動開啟和關閉、當我們執行一個SQL DML時、系統會自動開啟一個cursor、當執行完畢之後系統會關閉cursor、我們不能直接控制cursor、但是卻可以通過implicit Cursor的屬性來瞭解操作的狀態和結果、從而達到流程的控制——Cursor的屬性包括:
i、SQL%ROWCOUNT整形——代表DML語句成功執行的行數
ii、SQL%FOUND布爾型——值為true時代表插入、刪除、更新或查詢操作成功
iii、SQL%NOTFOUND布爾型——與上面相反
v、SQL%ISOPEN布爾型——DML執行過程中為真、否則為假
3)樣本:
begin
update student set sname='chy' WHERE sno='1';
if sql%isopen then
dbms_output.put_line('cursor is opening !');
else
dbms_output.put_line('cursor is closed !');
end if;
if sql%found then
dbms_output.put_line('DML is successed !');
else
dbms_output.put_line('DML is failed !');
end if;
if sql%notfound then
dbms_output.put_line('DML is failed !');
else
dbms_output.put_line('DML is successed !');
end if;
dbms_output.put_line(sql%rowcount||' is the number of result !');
exception
when no_data_found then
dbms_output.put_line('Sorry No data');
when too_many_rows then
dbms_output.put_line('Too Many rows');
end;
2、explicit cursor
1)explanation:PL/SQLalso lets you declare explicit cursors. An explicit cursor has a name and isassociated with a query (SQLSELECT statement)—usually one that returns multiplerows. After declaring an explicit cursor, you must open it (with the OPENstatement), fetch rows one at a time from the result set (with the FETCHstatement), and close the cursor (with the CLOSE statement). After closing thecursor, you can neither fetch records from the result set nor see the cursorattribute values.
很直白的說明了顯示遊標的用處、以及用法。
2)explicit cursor的屬性包含:
遊標的屬性傳回值類型意義
%ROWCOUNT 整型獲得FETCH語句返回的資料行數
%FOUND 布爾型最近的FETCH語句返回一行資料則為真,否則為假
%NOTFOUND 布爾型與%FOUND屬性傳回值相反
%ISOPEN 布爾型遊標已經開啟時值為真,否則為假
3)對於explicit Cursor使用分四個步驟:
a 定義遊標——Cursor [ Cursor Name[param_name, param_type]] IS select xxx from xxxwhere xxx;
b 開啟遊標——Open [ Cursor Name[varialbe_value] ] ;
c 操作遊標——Fetch [ Cursor Name ];
d 關閉遊標——Close [ Cursor Name ] ;
4)具體使用顯示遊標,遍曆迴圈遊標步驟:
a)使用顯示遊標
i、聲明遊標:劃分儲存地區,注意此時並沒有執行Select語句。CURSOR遊標名(參數列表) [傳回值類型] IS Select 語句;
ii、開啟遊標:執行Select語句,獲得結果集儲存到遊標中,此時遊標指向結果集頭,而不是第一條記錄。open遊標名(參數列表);
iii、擷取記錄��移動遊標取一條記錄 fetch 遊標名 into 臨時記錄或屬性類型變數;
v、關閉遊標:將遊標放入緩衝池中,沒有完全釋放資源。可重新開啟。
close 遊標名;
b)遍曆迴圈遊標
i、for迴圈遊標
迴圈遊標隱式開啟遊標,自動滾動擷取一條記錄,並自動建立臨時記錄類型變數儲存記錄。處理完後自動關閉遊標。
……
for 變數名 In 遊標名
loop
資料處理語句;
end loop;
ii、loop迴圈遊標
……
loop
fetch 遊標名 into 臨時記錄或屬性類型變數;
exit when 遊標名%notfound;
end loop;
iii、while迴圈
……
open 遊標名
fetch 遊標名into臨時記錄或屬性類型變數;
while 遊標名%foundloop
-- do something
fetch 遊標名into臨時記錄或屬性類型變數;
end loop;
……
close 遊標名
5)常見顯式Cursor用法:
i、使用for迴圈來使用cursor:
declare
cursor cur is select * from t_user where age = 22;
userinfo t_user%rowtype;
begin
for userinfo in cur loop
exit when cur%notfound;
dbms_output.put_line('user id : ' || userinfo.id || '-' || 'user name : ' || userinfo.username);
end loop;
exception
when others then
dbms_output.put_line(sqlerrm);
end;
ii、使用fetch來使用cursor: exp2
declare
cursor cur is select * from t_user where age = 22;
userinfo t_user%rowtype;
begin
open cur;
loop
exit when cur%notfound;
fetch cur into userinfo;
dbms_output.put_line('user id : ' || userinfo.id || '-' || 'user name : ' || userinfo.username);
end loop;
exception
when others then
dbms_output.put_line(sqlerrm);
close cur;
end;