oracle游标指针移动时机,oracle--游标(cursor)
1.游標(biāo)是:
一種PL/SQL控制結(jié)構(gòu),相當(dāng)于java中的Iterator ,它是指向結(jié)果集的指針。指向結(jié)果集第一條記錄的前一條,每次fetch 都會(huì)向下移動(dòng)下
游標(biāo)并不是一個(gè)數(shù)據(jù)庫(kù)對(duì)象,只是存留在內(nèi)存中
2. 作用:方便對(duì)表的行數(shù)據(jù)逐條進(jìn)行處理
---------------
--注意:fetch是下移動(dòng),%found是判斷此行有無(wú)數(shù)據(jù)的--這是作為循環(huán)的跳出條件的,定要注意--for循環(huán)除外
---注意:%found 與%notfound的區(qū)別--%found是判斷此行有沒有數(shù)據(jù),有的的時(shí)候執(zhí)行;%notfound 判斷當(dāng)前行是否有數(shù)據(jù),沒有則停止執(zhí)行
--在打開游標(biāo)之前最好先判斷游標(biāo)是否已打開? %isopen
eg:
IF mycur%ISOPEN THEN
null ;
ELSE
OPEN mycur ;
END IF ;
--可以使用ROWCOUNT對(duì)游標(biāo)所操作的行數(shù)進(jìn)行記錄。%rowcount
----------------
3.使用游標(biāo);
聲明游標(biāo)
cursor c is ??? --聲明
select * from emp;?? ---結(jié)果集
打開游標(biāo)
open c;---此時(shí)才會(huì)指向 結(jié)果集;--指向結(jié)果集第一條記錄之前。
循環(huán)抓取游標(biāo)
fetch c into 記錄類型的變量;---將抓取得游標(biāo)賦給一個(gè)記錄類型的變量
---每次只能讀取一行,若是想要遍歷,就要有循環(huán)。
---每當(dāng)遇到fetch 指針就會(huì)向下移動(dòng)一下。
關(guān)閉游標(biāo)
close c;
---
eg:
declare
cursor c is
select * from emp;
v_record_emp emp%rowtype;
v_sal emp.sal%type;
begin
open c;
loop
fetch c into v_record_emp;
exit when(c%notfound);
v_sal := v_record_emp.sal;
if(v_sal < 1200) then
v_record_emp.sal := v_sal + 500;
elsif(v_sal < 1500) then
v_record_emp.sal := v_sal + 300;
else
v_record_emp.sal := v_sal + 300;
end if;
update emp set sal=v_record_emp.sal where empno=v_record_emp.empno;
end loop;
close c;
commit;
end;
------------
使用for循環(huán)可以簡(jiǎn)化游標(biāo)使用時(shí)候的代碼(比較常用)
declare
cursor c is
select * from emp;
v_sal emp.sal%type;
begin
for v_record_emp in c loop---就完成了打開游標(biāo),抓取游標(biāo),和關(guān)閉游標(biāo)
v_sal := v_record_emp.sal;
if(v_sal < 1200) then
v_record_emp.sal := v_sal + 500;
elsif(v_sal < 1500) then
v_record_emp.sal := v_sal + 300;
else
v_record_emp.sal := v_sal + 300;
end if;
update emp set sal=v_record_emp.sal where empno=v_record_emp.empno;
end loop;
commit;
end;
-------------------
游標(biāo)分:
1.可更新的游標(biāo)
declare
cursor c is
select * from emp for update;--比普通的多? for update
begin
for v_record_emp in c loop
if(v_record_emp.sal<2000) then
update emp set sal=sal*2 where current of c;? --條件 current of c
elsif(v_record_emp.sal=5000) then
delete from emp where current of c;
end if;
end loop;
commit;
end;
----需要用? for update 來(lái)標(biāo)識(shí) 遍歷是給該結(jié)果集更新為目的的
----可更新的游標(biāo),我們能夠用current of c獲取到 當(dāng)前記錄
2.帶參數(shù)的游標(biāo)
declare
cursor c(v_deptno emp.deptno%type,v_job emp.job%type) is
select ename,sal from emp where deptno=v_deptno and job=v_job;
begin
for v_temp in c(30,'CLERK') loop
dbms_output.put_line(v_temp.ename);
end loop;
end;
---帶參數(shù)的目的是為了? 設(shè)置獲取參數(shù)的條件
---游標(biāo)的參數(shù)是在open游標(biāo)的時(shí)候賦值的---open c(30,'clerk');
3.隱士游標(biāo)
當(dāng)用戶在PL/SQL 中使用數(shù)據(jù)操縱語(yǔ)句(DML)時(shí),
oracle預(yù)先定義一個(gè)名稱為SQL的隱式游標(biāo),
通過(guò) 檢查隱式游標(biāo)的屬性獲取與最近執(zhí)行的SQL語(yǔ)句相關(guān)信息。
在執(zhí)行DML語(yǔ)句之后,隱式游標(biāo)屬性返回信息。
隱式游標(biāo)屬性包括: %found %notfound %rowcount %isopen
eg: sql%rowcount中的sql是oracle的內(nèi)部游標(biāo),rowcount的意思是之前的dml sql語(yǔ)句影響的多少行數(shù)據(jù)。
eg:
declare
begin
update B_BARCODE_JINZHI_T set jinzhi='00';
dbms_output.put_line(to_char(sql%rowcount));
end;
---結(jié)果: 5
總結(jié)
以上是生活随笔為你收集整理的oracle游标指针移动时机,oracle--游标(cursor)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: db2与oracle的区别 锁,db2和
- 下一篇: oracle show选项,Oracle