oracle cursor 學習小結

2021-04-22 16:02:05 字數 4333 閱讀 3058

游標:用來查詢資料庫,獲取記錄集合(結果集)的指標,可以讓開發者一次訪問一行結果集,在每條結果集上作操作。

二.分類:

1.靜態游標:

分為顯式游標和隱式游標。

2.ref游標:

是一種引用型別,類似於指標。

三.詳細內容:

1.顯式游標:

cursor游標名(引數) [返回值型別] is

select語句

生命週期:

a.開啟游標(open)

解析,繫結。。。不會從資料庫檢索資料

b.從游標中獲取記錄(fetch into)

執行查詢,返回結果集。通常定義局域變數作為從游標獲取資料的緩衝區。

c.關閉游標(close)

完成游標處理,使用者不能從游標中獲取行。還可以重新開啟。

選項:引數和返回型別

set serveroutput on

declare

cursor emp_cur ( p_deptid in number) is

select * from employees where department_id = p_deptid;

l_emp employees%rowtype;

begin

dbms_output.put_line(''getting employees from department 30'');

open emp_cur(30);

loop

fetch emp_cur into l_emp;

exit when emp_cur%notfound;

dbms_output.put_line(''employee id ''|| l_emp.employee_id || '' is '');

dbms_output.put_line(l_emp.first_name || '' '' || l_emp.last_name);

end loop;

close emp_cur;

dbms_output.put_line(''getting employees from department 90'');

open emp_cur(90);

loop

fetch emp_cur into l_emp;

exit when emp_cur%notfound;

dbms_output.put_line(''employee id ''|| l_emp.employee_id || '' is '');

dbms_output.put_line(l_emp.first_name || '' '' || l_emp.last_name);

end loop;

close emp_cur;

end;

/2.隱式游標:

不用明確建立游標變數,分兩種:

a.在pl/sql中使用dml語言,使用oracle提供的名為sql

的隱示游標

b.cursor for loop,用於for loop語句

a.舉例:

declare

begin

update departments set department_name=department_name;

--where 1=2;

dbms_output.put_line(''update ''|| sql%rowcount ||'' records'');

end;/

b.舉例:

declare

begin

for my_dept_rec in ( select department_name, department_id from departments)

loop

dbms_output.put_line(my_dept_rec.department_id || '' : '' || my_dept_rec.department_name);

end loop;

end;

/c.舉例:

單獨select

declare

l_empno emp.employee_id%type;

-- l_ename emp.ename%type;

begin

select employee_id

into l_empno

from emp;

--where rownum =1;

dbms_output.put_line(l_empno);

end;

/使用into獲取值,只能返回一行。

游標屬性:

%found:變數最後從游標中獲取記錄的時候,在結果集中找到了記錄。

%notfound:變數最後從游標中獲取記錄的時候,在結果集中沒有找到記錄。

%rowcount:當前時刻已經從游標中獲取的記錄數量。

%isopen:是否開啟。

declare

cursor emps is

select * from employees where rownum<6 order by 1;

emp employees%rowtype;

row number :=1;

begin

open emps;

fetch emps into emp;

loop

if emps%found then

dbms_output.put_line(''looping over record ''||row|| '' of '' || emps%rowcount);

fetch emps into emp;

row := row + 1;

elsif emps%notfound then

exit; ---exit loop, not if

end if;

end loop;

if emps%isopen then

close emps;

end if;

end;/

顯式和隱式游標的區別:

盡量使用隱式游標,避免編寫附加的游標控制**(宣告,開啟,獲取,關閉),也不需要宣告變數來儲存從游標中獲取的資料。

3.ref cursor游標:

動態游標,在執行的時候才能確定游標使用的查詢。分類:

1.強型別(限制)ref cursor,規定返回型別

2.弱型別(非限制)ref cursor,不規定返回型別,可以獲取任何結果集。

type ref_cursor_name is ref cursor [return return_type]

declare

type refcur_t is ref cursor;

type emp_refcur_t is ref cursor return employee%rowtype;

begin

null;

end;

/強型別舉例:

declare

--宣告記錄型別

type emp_job_rec is record(

employee_id number,

employee_name varchar2(50),

job_title varchar2(30)

);--宣告ref cursor,返回值為該記錄型別

type emp_job_refcur_type is ref cursor

return emp_job_rec;

--定義ref cursor游標的變數

emp_refcur emp_job_refcur_type;

emp_job emp_job_rec;

begin

open emp_refcur for

select e.employee_id,

e.first_name || '' '' ||e.last_name "employee_name",

j.job_title

from employees e, jobs j

where e.job_id = j.job_id and rownum < 11 order by 1;

fetch emp_refcur into emp_job;

while emp_refcur%found loop

dbms_output.put_line(emp_job.employee_name || ''''''s job is '');

dbms_output.put_line(emp_job.job_title);

fetch emp_refcur into emp_job;

end loop;

end;

/

oracle cursor 學習小結

一 概念 游標 用來查詢資料庫,獲取記錄集合 結果集 的指標,可以讓開發者一次訪問一行結果集,在每條結果集上作操作。二 分類 1 靜態游標 分為顯式游標和隱式游標。2 ref 游標 是一種引用型別,類似於指標。三 詳細內容 1 顯式游標 cursor 游標名 引數 返回值型別 is select 語...

oracle cursor 簡單用法

procedure changespecialdiscount compid in invarchar2 ccid in invarchar2 cono in invarchar2 is sum cc all number 14 4 0 wsp disc number 14 4 wspcl disc...

Oracle Cursor游標的使用

1 宣告游標 2 開啟游標 3 逐行讀取游標中的資料,進行處理。4 關閉游標 open 開啟游標 found 判斷有沒有受影響的資料 notfound 判斷有沒有受影響的資料 isopen 判斷游標是否開啟 rowcount 返回受影響的行數 fetch 向下移動一步游標 close 關閉游標隱式游...