|
數(shù)據(jù)庫(kù)中的游標(biāo)(以下內(nèi)容以Oracle為例): 游標(biāo)是sql的一個(gè)內(nèi)存工作區(qū),由系統(tǒng)或用戶以變量的形式定義 游標(biāo)的作用就是用于臨時(shí)存儲(chǔ)從數(shù)據(jù)庫(kù)中提取的數(shù)據(jù)塊,通俗的講游標(biāo)就是一個(gè)結(jié)果集; 游標(biāo)的屬性: %found:用于檢測(cè)游標(biāo)結(jié)果集是否存在數(shù)據(jù),如果存在,則返回true; %notfound:用于檢測(cè)游標(biāo)結(jié)果集是否存在數(shù)據(jù),如果不存在,則返回true; %isopen:用于檢測(cè)游標(biāo)是否打開(kāi),如果打開(kāi),則返回true; %rowcount:用于返回已提取的實(shí)際行數(shù);例,當(dāng)提取5行數(shù)據(jù)時(shí)關(guān)閉游標(biāo); 常見(jiàn)游標(biāo)分類: 顯式游標(biāo)、隱式游標(biāo) 顯式游標(biāo)的定義步驟: 聲明游標(biāo) declare cursor cursor_name[(parameter_name datatype)] is select_statement cursor_name emp%rowtype; 打開(kāi)游標(biāo) open cursor_name 提取數(shù)據(jù) fetch cursor_name into variable1... 循環(huán)提?。?/p> loop exit when cursor_name%notfound end loop; ----------------------------------或者 while cursor_name%found loop end loop; 關(guān)閉游標(biāo) close cursor_name 隱式游標(biāo):由系統(tǒng)隱含創(chuàng)建的游標(biāo),主要用于非查詢語(yǔ)句;隱式游標(biāo)的名字為sql,這是由oracle系統(tǒng)定義的;系統(tǒng)會(huì)自動(dòng)打開(kāi)游標(biāo)、提取數(shù)據(jù)、關(guān)閉游標(biāo)等操作; 主要應(yīng)用于:DML操作和select...into...的單行查詢語(yǔ)句; 隱式游標(biāo)的屬性:通過(guò)sql游標(biāo)名總是只能訪問(wèn)前一個(gè)DML操作或單行select操作的游標(biāo)屬性; sql%found:為true時(shí),表示DML或單行SELECT操作成功 sql%notfound sql%isopen:DML操作執(zhí)行過(guò)程中,為true;結(jié)束為false; sql%rowcound:DML成功執(zhí)行后的數(shù)據(jù)的行數(shù); 例:根據(jù)用戶輸入的員工號(hào),更新指定員工的工資(+100); begin DML操作語(yǔ)句; if sql%found then 執(zhí)行語(yǔ)句并提交事務(wù); else 執(zhí)行語(yǔ)句并回滾事務(wù); end if; end; |
|
|
來(lái)自: 好程序員IT > 《Java培訓(xùn)教程》