--Oracle?PL/SQL

創(chuàng)新互聯(lián)是網(wǎng)站建設(shè)專家,致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營(yíng)銷,專業(yè)領(lǐng)域包括成都做網(wǎng)站、成都網(wǎng)站建設(shè)、電商網(wǎng)站制作開發(fā)、重慶小程序開發(fā)、微信營(yíng)銷、系統(tǒng)平臺(tái)開發(fā),與其他網(wǎng)站設(shè)計(jì)及系統(tǒng)開發(fā)公司不同,我們的整合解決方案結(jié)合了恒基網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗(yàn)和互聯(lián)網(wǎng)整合營(yíng)銷的理念,并將策略和執(zhí)行緊密結(jié)合,且不斷評(píng)估并優(yōu)化我們的方案,為客戶提供全方位的互聯(lián)網(wǎng)品牌整合方案!
declare
--定義游標(biāo)
cursor?cur_test?is
select?*?from?emp;
v_emp?emp%rowtype;
begin
--打開游標(biāo)
open?cur_test;
loop
--獲取游標(biāo)值
fetch?cur_test
into?v_emp;
exit?when?cur_test%notfound;--屬性為是否提取數(shù)據(jù)成功,不成功則TRUE
dbms_output.put_line(v_emp.empno?||?'_'?||?v_emp.ename);
end?loop;
--關(guān)閉游標(biāo)
close?cur_test;
end;
在你調(diào)用這個(gè)存儲(chǔ)過程之后游標(biāo)就打開了,調(diào)用存儲(chǔ)過程之后fetch出這個(gè)游標(biāo)的值,再顯示的關(guān)閉游標(biāo),顯示的打開就要顯示的關(guān)閉,
游標(biāo)能夠根據(jù)查詢條件從數(shù)據(jù)表中提取一組記錄,將其作為一個(gè)臨時(shí)表置于數(shù)據(jù)緩沖區(qū)中,利用指針逐行對(duì)記錄數(shù)據(jù)進(jìn)行操作。
Oracle中的游標(biāo)分為顯示游標(biāo)和隱式游標(biāo) 。
在執(zhí)行SQL語句時(shí),Oracle會(huì)自動(dòng)創(chuàng)建隱式游標(biāo),該游標(biāo)是內(nèi)存中處理該語句的數(shù)據(jù)緩沖區(qū),存儲(chǔ)了執(zhí)行SQL語句的結(jié)果。通過隱式游標(biāo)屬性可獲知SQL語句的執(zhí)行狀態(tài)信息。
%found:布爾型屬性,如果sql語句至少影響到一行數(shù)據(jù),值為true,否則為false。
%notfound:布爾型屬性,與%found相反。
%rowcount:數(shù)字型屬性,返回受sql影響的行數(shù)。
%isopen:布爾型屬性,當(dāng)游標(biāo)已經(jīng)打開時(shí)返回true,游標(biāo)關(guān)閉時(shí)則為false。
用戶可以顯式定義游標(biāo)。使用顯式游標(biāo)處理數(shù)據(jù)要4個(gè)步驟:定義游標(biāo)、打開游標(biāo)、提取游標(biāo)數(shù)據(jù)和關(guān)閉游標(biāo)。
游標(biāo)由游標(biāo)名稱和游標(biāo)對(duì)應(yīng)的select結(jié)果集組成。定義游標(biāo)應(yīng)該放在pl/sql程序塊的聲明部分。
語法格式:cursor 游標(biāo)名稱(參數(shù)) is 查詢語句
打開游標(biāo)時(shí),游標(biāo)會(huì)將符合條件的記錄送入數(shù)據(jù)緩沖區(qū),并將指針指向第一條記錄。
語法格式:open 游標(biāo)名稱(參數(shù));
將游標(biāo)中的當(dāng)前行數(shù)據(jù)賦給指定的變量或記錄變量。
語法格式:fetch 游標(biāo)名稱 into 變量名;
游標(biāo)一旦使用完畢,就應(yīng)將其關(guān)閉,釋放與游標(biāo)相關(guān)聯(lián)的資源。
語法格式:close 游標(biāo)名稱;
declare
cursor c1 is? select sno,cno,grade from sc;
v_sno sc.sno%type;
v_cno sc.cno%type;
v_grade sc.grade%type;
begin
open c1;
loop
? fetch c1 into v_sno,v_cno,v_grade;
? exit when c1%notfound;--緊跟fetch之后
if c1%found then
dbms_output.put_line(to_char(c1%rowcount)||v_cno);
end if;
end loop;
close c1;?
end;
declare
cursor c1 is select sno,cno,grade from sc;
v_sno sc.sno%type;
v_cno sc.cno%type;
v_grade sc.grade%type;
begin
open c1;
fetch c1 into v_sno,v_cno,v_grade;
while c1%found loop
? dbms_output.put_line(v_sno||v_cno||v_grade);
?fetch c1 into v_sno,v_cno,v_grade;
end loop;
close c1;?
end;
第三種:for
declare
cursor c1 is select sno,cno,grade from sc;
begin
for item in c1 loop
dbms_output.put_line(rpad(item.sno,'10',' ')||rpad(item.cno,'10',' ')||rpad(item.grade,'10',' '));
end loop;
end;
1. 用open打開的,用close關(guān)閉\x0d\x0adeclare\x0d\x0acursor mycursor is\x0d\x0aselect * from emp for update;\x0d\x0amyrecord emp%rowtype;\x0d\x0abegin\x0d\x0aopen mycursor;\x0d\x0aloop\x0d\x0afetch mycursor into myrecord;\x0d\x0aexit when mycursor%notfound;\x0d\x0aif (myrecord.sal=2000) then\x0d\x0aupdate emp\x0d\x0aset sal=2001\x0d\x0awhere current of mycursor;\x0d\x0aend if;\x0d\x0aend loop;\x0d\x0aclose mycursor;\x0d\x0acommit;\x0d\x0aend;\x0d\x0a2. 用for 循環(huán)的,循環(huán)完了就自己關(guān)了\x0d\x0adeclare\x0d\x0acursor mycursor is\x0d\x0aselect * from emp;\x0d\x0abegin\x0d\x0afor i in mycursor\x0d\x0aloop\x0d\x0adbms_output.put_line(i.job);\x0d\x0aend loop;\x0d\x0aend;
文章標(biāo)題:oracle游標(biāo)怎么關(guān),oracle 游標(biāo)使用
本文URL:http://chinadenli.net/article0/hsphio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、用戶體驗(yàn)、品牌網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、Google、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)