遊標是從資料表中提取出來的資料,以暫存資料表的形式存放在記憶體中,在遊標中有一個資料指標,在初始狀態下指向的是首記錄,利用fetch語句可以移動該指標,從而對遊標中的資料進行各種操作,然後將操作結果寫回資料表中。如下例子注釋與說明: set serveroutput on
declare
sh_no tssa.entry_sheet_td.sheet_no%type; --該程式定義sh_no為與tssa.entry_sheet_td資料表中的sheet_no欄位類型相同的變數
v_flow_inst tssa.entry_sheet_td.PROCESSINSTID%type;
cursor mycursor is
select sheet_no from entry_sheet_td a ,wfprocessinst b where a.processinstid=b.processinstid and b.currentstate='7';
cursorrecord mycursor%rowtype; --mycursor為從entry_sheet_td資料表中提取的接入型障礙中流程結束而未歸檔工單資料構成的遊標,cursorrecord mycursor%rowtype定義記錄變數
begin
sh_no:='';
open mycursor; --開啟遊標,開啟遊標的過程有以下兩個步驟:1、將合格記錄送入記憶體,2、將指標指向第一條記錄。
loop
fetch mycursor into cursorrecord; --要提取遊標中的資料,使用fetch命令,文法形式如:fetch 遊標名 into 變數名1, 變數名2,……;
exit when mycursor%notfound; --噹噹前迴圈的記錄值為空白時,即迴圈結束後,退出迴圈
sh_no:=cursorrecord.sheet_no;
dbms_output.put_line(sh_no); --輸出當前滿足條件的工單號,此處可以進行一些邏輯處理 end loop;
close mycursor; --關閉遊標
end;上面只是對遊標寫法的一個簡要說明。