Cursor游標(游標)的使用

2021-07-02 04:51:55 字數 3997 閱讀 9228

為了處理sql語句,oracle

將在記憶體中分配乙個區域,這就是上下文區。這個區包含了已經處理完的行數、指向被分析語句的指標,整個區是查詢語句返回的資料行集。游標就是指向上下文區控制代碼或指標。

兩種游標:

一、顯示游標(需要明確定義!)

顯示游標被用於處理返回多行資料的select 語句,游標名通過cursor….is 語句顯示地賦給select 語句。

在pl/sql中處理顯示游標所必需的四個步驟:

1)宣告游標;

cursor cursor_name is select_statement

2)為查詢開啟游標;

open cursor_name

3)取得結果放入pl/sql變數中;

fetch cursor_name into list_of_variables;

fetch cursor_name into pl/sql_record;

4)關閉游標。

close cursor_name

注意:在宣告游標時,

select_statement不能包含into子句。當使用顯示游標時,into子句是fetch語句的一部分。

1、 顯式游標

select語句上 使用顯式游標

能明確訪問結果集

for迴圈游標

引數游標 

解決多行記錄的查詢問題

fetch游標

二、隱式游標

所有的隱式游標都被假設為只返回一條記錄。

使用隱式游標時,使用者無需進行宣告、開啟及關閉。pl/sql隱含地開啟、處理,然後關掉游標。

例如:…….

select studentno,studentname

into curstudentno,curstudentname

from studentrecord

where name=』gg』;

上述游標自動開啟,並把相關值賦給對應變數,然後關閉。執行完後,pl/sql變數curstudentno,curstudentname中已經有了值。

2、 隱式游標

單條sql語句所產生的結果集合 

用關鍵字sql表示隱式游標

4個屬性 %rowcount  影響的記錄的行數  整數

%found     影響到了記錄 true

%notfound  沒有影響到記錄 true

%isopen    是否開啟  布林值 永遠是false

多條sql語句 隱式游標sql永遠指的是最後一條sql語句的結果

主要使用在update 和 delete語句上

實際操作和例子:

(1)for迴圈游標 (常用的一種游標)

--<1>定義游標

--<2>定義游標變數

--<3>使用for迴圈來使用這個游標

--前向游標 只能往乙個方向走

--效率很高

declare

--型別定義

cursor cc is select empno,ename,job,sal

from emp where job = 'manager';

--定義乙個游標變數

ccrec cc%rowtype;

begin

--for迴圈

for ccrec in cc loop

dbms_output.put_line(ccrec.empno||'-'||ccrec.ename||'-'||ccrec.job||'-'||ccrec.sal);

end loop;       

end;

(2) fetch游標

--使用的時候 必須要明確的開啟和關閉

declare

--型別定義

cursor cc is select empno,ename,job,sal

from emp where job = 'manager';

--定義乙個游標變數

ccrec cc%rowtype;

begin

--開啟游標

open cc;

--loop迴圈

loop

--提取一行資料到ccrec中 

fetch cc into ccrec;         

--判斷是否提取到值,沒取到值就退出

--取到值cc%notfound 是false

--取不到值cc%notfound 是true 

exit when cc%notfound;

dbms_output.put_line(ccrec.empno||'-'||ccrec.ename||'-'||ccrec.job||'-'||ccrec.sal);            

end loop; 

--關閉游標

close cc;  

end;

游標的屬性4種

%notfound  fetch是否提到資料 沒有true 提到false

%found      fetch是否提到資料 有true 沒提到false

%rowcount  已經取出的記錄的條數

%isopen    布林值 游標是否開啟

(3)引數游標

按部門編號的順序輸出部門經理的名字

declare

--部門

cursor c1 is select deptno from dept;

--引數游標c2,定義引數的時候

--只能指定型別,不能指定長度  

--引數只能出現在select語句=號的右側

cursor c2(no number,pjob varchar2) is select emp.* from emp

where deptno = no and job=pjob;

c1rec c1%rowtype;

c2rec c2%rowtype;

--定義變數的時候要指定長度

v_job varchar2(20);

begin

--部門

for c1rec in c1 loop

--引數在游標中使用

for c2rec in c2(c1rec.deptno,'manager') loop

dbms_output.put_line(c1rec.deptno||'-'||c2rec.ename);

end loop; 

end loop; 

end; 

(4)引用游標/動態游標

-- select語句是動態的

declare

--定義乙個型別(ref cursor)弱型別    

type cur is ref cursor;

--強型別(返回的結果集有要求)

type cur1 is ref cursor return emp%rowtype;

--定義乙個ref cursor型別的變數   

cura cur;

c1rec emp%rowtype;

c2rec dept%rowtype;

begin

dbms_output.put_line('輸出員工')   ;       

open cura for select * from emp;

loop

fetch cura into c1rec;   

exit when cura%notfound;

dbms_output.put_line(c1rec.ename)   ;

end loop ;

dbms_output.put_line('輸出部門')   ;

open cura for select * from dept;

loop

fetch cura into c2rec;   

exit when cura%notfound;

dbms_output.put_line(c2rec.dname)   ;

end loop;  

close cura;

end;

PL SQL中cursor 游標 游標 的用法

今天簡單的總結一下pl sql中cursor 游標 游標 的用法.相信不少做開發或維護的dba在找工作的時候,遇到過類似的面視問題 請簡單的描述一下游標的型別,說一下普通游標和ref游標之間的區別,以及什麼時候該正確應用哪乙個?這個題目,我著實難住了不少人,其實他們在具體開發的時候,也還是比較能夠把...

游標的使用

declare sql varchar 8000 password varchar 200 密碼 tbname varchar 500 資料庫.dbo.表名,如果不指定 dbo.表名,則匯出資料庫的所有使用者表 filename varchar 1000 匯入 匯出路徑 檔名,如果 tbname引數...

游標的使用

游標 cursor 是處理資料的一種方法,為了檢視或者處理結果集中的資料,游標提供了在結果集中一次以行或者多行前進或向後瀏覽資料的能力。我們可以把游標當作乙個指標,它可以指定結果中的任何位置,然後允許使用者對指定位置的資料進行處理。1.游標的組成 游標包含兩個部分 乙個是游標結果集 乙個是游標位置。...