Oracle 游標操作

2021-07-16 05:21:06 字數 2560 閱讀 7588

sql語言分為六種,其中ccl:cursor control language游標控制語言

簡單定義:游標是指向結果集的指標,類似迭代器iterator

一開始指向結果集的第一條記錄之前的記錄,每fetch一次往下移動一條記錄,返回指標指向的當前記錄。

游標的操作

(1) 宣告游標—–>cursor c is selsect * from emp

(2) 開啟游標——>open c;

(3) 迭代抓取游標——>用迴圈遍歷

(4) 最後關閉游標——>close c;

declare

--1,宣告游標

cursor c is

select * from emp;

v_emprow emp%rowtype;

begin

--2,開啟游標

open c;

--3,迴圈抓取游標

loop

fetch c into v_emprow;

exit

when(c%notfound);

--必須要寫到exit when 後面

dbms_output.put_line(v_emprow.ename);

endloop;

--4,關閉游標

close c;

end;

用whil迴圈遍歷

declare

--1,宣告游標

cursor c is

select * from emp;

v_emprow emp%rowtype;

begin

--2,開啟游標

open c;

--3,迴圈抓取游標

fetch c into v_emprow;

while(c%found) loop

dbms_output.put_line(v_emprow.ename);

fetch c into v_emprow;

end loop;

--4,關閉游標

close c;

end;

使用for迴圈遍歷,for迴圈會自動開啟游標,自動迭代抓取游標,自動關閉游標。

declare

--1,宣告游標

cursor c is

select * from emp;

begin

for v_emprow in c loop

dbms_output.put_line(v_emprow.ename);

end loop;

end;

declare

-- 查詢部門編號為 10的, 工種為 'clerk'的職員的姓名

cursor c(v_deptno emp.deptno%type,v_job emp.job%type) is

select * from emp where deptno=v_deptno and job=v_job;

--v_emp emp%rowtype;

begin

for v_emp in c(10,'clerk') loop

dbms_output.put_line(v_emp.ename);

end loop;

end;

可更新的游標…………for update ……………current of………………..

declare

--1,宣告游標

cursor c is

select * from emp1 for

update;

begin

for v_emprow in c loop

update emp1 set sal=sal*2

where

current

of c;

end loop;

end;

隱式游標

sql%rowcount 返回最後一條sql語句影響的行數,檢視操作是否成功,

declare

--1,宣告游標

v_count number(2);

cursor c is

select * from emp1 for

update;

begin

for v_emprow in c loop

update emp1 set sal=sal*2

where

current

of c;

end loop;

--dbms_output.put_line(''||rowcount);

v_count:=sql%rowcount;

dbms_output.put_line(v_count);

end;

oracle 游標操作,cursor

在游標中使用引數 cursor cursor name p state in state type is select statement 沒有引數的寫法是 cursor cursor name is select statement 對於括號裡面的,in 左邊是引數的別名,in 右邊是引數的型別,...

Oracle中的游標操作

游標是oracle系統在記憶體中開闢的乙個工作區,在其中存放select語句返回的查詢結果 1 游標操作的過程 1 定義游標 2 開啟游標 3 游標的操作 移動,讀取資料 4 關閉游標 注釋 游標的定義格式 cursor cusor name is select語句 游標的開啟 open 游標名 游...

Oracle 游標 以及CLOB的簡單操作

sample declare cursor mycursor is select from auto event where event id between 54652 and 54681 begin for myoncursor in mycursor loop if myoncursor.st...