日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

oracle游标指针移动时机,oracle--游标(cursor)

發布時間:2024/10/14 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 oracle游标指针移动时机,oracle--游标(cursor) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.游標是:

一種PL/SQL控制結構,相當于java中的Iterator ,它是指向結果集的指針。指向結果集第一條記錄的前一條,每次fetch 都會向下移動下

游標并不是一個數據庫對象,只是存留在內存中

2. 作用:方便對表的行數據逐條進行處理

---------------

--注意:fetch是下移動,%found是判斷此行有無數據的--這是作為循環的跳出條件的,定要注意--for循環除外

---注意:%found 與%notfound的區別--%found是判斷此行有沒有數據,有的的時候執行;%notfound 判斷當前行是否有數據,沒有則停止執行

--在打開游標之前最好先判斷游標是否已打開? %isopen

eg:

IF mycur%ISOPEN THEN

null ;

ELSE

OPEN mycur ;

END IF ;

--可以使用ROWCOUNT對游標所操作的行數進行記錄。%rowcount

----------------

3.使用游標;

聲明游標

cursor c is ??? --聲明

select * from emp;?? ---結果集

打開游標

open c;---此時才會指向 結果集;--指向結果集第一條記錄之前。

循環抓取游標

fetch c into 記錄類型的變量;---將抓取得游標賦給一個記錄類型的變量

---每次只能讀取一行,若是想要遍歷,就要有循環。

---每當遇到fetch 指針就會向下移動一下。

關閉游標

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循環可以簡化游標使用時候的代碼(比較常用)

declare

cursor c is

select * from emp;

v_sal emp.sal%type;

begin

for v_record_emp in c loop---就完成了打開游標,抓取游標,和關閉游標

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;

-------------------

游標分:

1.可更新的游標

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 來標識 遍歷是給該結果集更新為目的的

----可更新的游標,我們能夠用current of c獲取到 當前記錄

2.帶參數的游標

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;

---帶參數的目的是為了? 設置獲取參數的條件

---游標的參數是在open游標的時候賦值的---open c(30,'clerk');

3.隱士游標

當用戶在PL/SQL 中使用數據操縱語句(DML)時,

oracle預先定義一個名稱為SQL的隱式游標,

通過 檢查隱式游標的屬性獲取與最近執行的SQL語句相關信息。

在執行DML語句之后,隱式游標屬性返回信息。

隱式游標屬性包括: %found %notfound %rowcount %isopen

eg: sql%rowcount中的sql是oracle的內部游標,rowcount的意思是之前的dml sql語句影響的多少行數據。

eg:

declare

begin

update B_BARCODE_JINZHI_T set jinzhi='00';

dbms_output.put_line(to_char(sql%rowcount));

end;

---結果: 5

總結

以上是生活随笔為你收集整理的oracle游标指针移动时机,oracle--游标(cursor)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。