十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
Oracle游標(biāo)分為顯示游標(biāo)和隱式游標(biāo)
創(chuàng)新互聯(lián)是專業(yè)的黟縣網(wǎng)站建設(shè)公司,黟縣接單;提供網(wǎng)站制作、成都網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行黟縣網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!
顯示游標(biāo)(Explicit Cursor):在PL/SQL程序中定義的 用于查詢的游標(biāo)稱作顯示游標(biāo)
隱式游標(biāo)(Implicit Cursor):是指非PL/SQL程序中定義的 而且是在PL/SQL中使用UPDATE/DELETE語句時 Oracle系統(tǒng)自動分配的游標(biāo)
一 顯示游標(biāo)
使用步驟
( )定義 ( )打開 ( )使用 ( )關(guān)閉
使用演示
首先創(chuàng)建測試用表STUDENT 腳本如下
( ) 使用WHILE循環(huán)處理游標(biāo)
create or replace PROCEDURE PROC_STU AS
BEGIN
顯示游標(biāo)使用 使用while循環(huán)
declare
定義游標(biāo) 名稱為cur_stu
cursor cur_stu is
select stuno stuname from student order by stuno;
定義變量 存放游標(biāo)取出的數(shù)據(jù)
v_stuno varchar( );
v_stuname varchar( );
begin
打開游標(biāo)cur_stu
open cur_stu;
將游標(biāo)的當(dāng)前行取出存放到變量中
fetch cur_stu into v_stuno v_stuname;
while cur_stu%found 游標(biāo)所指還有數(shù)據(jù)行 則繼續(xù)循環(huán)
loop
打印結(jié)果
dbms_output PUT_LINE(v_stuno|| ||v_stuname);
繼續(xù)將游標(biāo)所指的當(dāng)前行取出放到變量中
fetch cur_stu into v_stuno v_stuname;
end loop;
close cur_stu; 關(guān)閉游標(biāo)
end;
END PROC_STU ;
( ) 使用IF ELSE代替WHILE循環(huán)處理游標(biāo)
create or replace PROCEDURE PROC_STU AS
BEGIN
顯示游標(biāo)使用 使用if判斷
declare
定義游標(biāo) 名稱為cur_stu
cursor cur_stu is
select stuno stuname from student order by stuno;
定義變量 存放游標(biāo)取出的數(shù)據(jù)
v_stuno varchar( );
v_stuname varchar( );
begin
打開游標(biāo)cur_stu
open cur_stu;
將游標(biāo)的當(dāng)前行取出存放到變量中
fetch cur_stu into v_stuno v_stuname;
loop
if cur_stu%found then 如果游標(biāo)cur_stu所指還有數(shù)據(jù)行
打印結(jié)果
dbms_output PUT_LINE(v_stuno|| ||v_stuname);
繼續(xù)將游標(biāo)所指的當(dāng)前行取出放到變量中
fetch cur_stu into v_stuno v_stuname;
else
exit;
end if;
end loop;
close cur_stu; 關(guān)閉游標(biāo)
end;
END PROC_STU ;
( ) 使用FOR循環(huán)處理游標(biāo)
create or replace PROCEDURE PROC_STU AS
BEGIN
顯示游標(biāo)使用 使用for循環(huán)
declare
定義游標(biāo) 名稱為cur_stu
cursor cur_stu is
select stuno stuname from student order by stuno;
begin
for stu in cur_stu
loop
dbms_output PUT_LINE(stu stuno|| ||stu stuname);
循環(huán)做隱含檢查 %notfound
end loop;
自動關(guān)閉游標(biāo)
end;
END PROC_STU ;
( ) 常用的使用EXIT WHEN處理游標(biāo)
create or replace
PROCEDURE PROC_STU _ AS
BEGIN
顯示游標(biāo)使用 使用exit when循環(huán)
declare
定義游標(biāo) 名稱為cur_stu
cursor cur_stu is
select stuno stuname from student order by stuno;
定義變量 存放游標(biāo)取出的數(shù)據(jù)
v_stuno varchar( );
v_stuname varchar( );
begin
打開游標(biāo)cur_stu
open cur_stu;
loop
將游標(biāo)的當(dāng)前行取出存放到變量中
fetch cur_stu into v_stuno v_stuname;
exit when cur_stu%notfound; 游標(biāo)所指還有數(shù)據(jù)行 則繼續(xù)循環(huán)
打印結(jié)果
dbms_output PUT_LINE(v_stuno|| ||v_stuname);
end loop;
close cur_stu; 關(guān)閉游標(biāo)
end;
END PROC_STU _ ;
二 隱式游標(biāo)
使用演示
create or replace PROCEDURE PROC_STU AS
BEGIN
隱式游標(biāo)使用
update student set stuname= 張燕廣 where stuno= ;
如果更新沒有匹配則插入一條新記錄
if SQL%NOTFOUND then
insert into student(STUNO STUNAME AGE GENDER)
values( 張燕廣 男 );
end if;
END PROC_STU ;
說明
所有的SQL語句在上下文區(qū)內(nèi)部都是可執(zhí)行的 因為都有一個游標(biāo)指向上下文區(qū) 此游標(biāo)就是
SQL游標(biāo) 與現(xiàn)實游標(biāo)不同的是 SQL游標(biāo)在PL/SQL中不需要打開和關(guān)閉 而是在執(zhí)行UPDATE
DELETE是自動打開和關(guān)閉
上面例子中就是通過SQL%NOTFOUND游標(biāo)屬性判斷UPDATE語句的執(zhí)行結(jié)果決定是否需要插入新記錄 CREATE TABLE STUDENT (
STUNAME VARCHAR ( BYTE)
STUNO VARCHAR ( BYTE)
AGE NUMBER
GENDER VARCHAR ( CHAR)
lishixinzhi/Article/program/Oracle/201311/17531
--Oracle?PL/SQL
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;
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;