oracle游標使用

2021-08-23 15:54:13 字數 4396 閱讀 8630

游標

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

分類

靜態游標:

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

ref游標:

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

顯式游標

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

select 語句

生命週期:

1.開啟游標(open)

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

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

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

3.關閉游標(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;/

隱式游標

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

1.在pl/sql中使用dml語言,使用oracle提供的名為sql的隱示游標

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

1舉例:

declare

begin

update departments set department_name=department_name;

--where 1=2;

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

end;/

2舉例:

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;/

3舉例:

單獨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;/

顯式和隱式游標的區別

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

ref cursor游標

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

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

弱型別(非限制)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 游標使用

create or replace function errortyperead return varchar2 is result varchar2 3000 type cursor type is ref cursor tempname varchar2 100 cursor testcur i...

oracle游標使用

在進行pl sql程式設計時,我們都會使用游標,游標有兩種,一種是顯式游標,使用類似如下方式 open 游標 loop fetch into exit when notfound end loop close 游標 另一種是隱式游標,使用類似如下 for 游標變數 in 游標 loop 賦值變數 游...

Oracle游標使用

一,什麼是游標 遍歷查詢結果集的一種機制。二,為什麼避免使用游標 盡量避免使用游標,因為游標的效率較差 如果使用了游標,就要盡量避免在游標迴圈中再進行表連線的操作。三,使用游標的步驟 靜態游標 1,宣告一些變數,用來儲存游標遍歷過程的臨時資料 2,宣告游標,並且指定查詢 3,開啟游標 4,從游標中獲...