oracle 游標使用

2021-08-31 10:38:19 字數 3815 閱讀 9176

游標(cursor)也叫游標,在關聯式資料庫中經常使用,在pl/sql程式中可以用cursor與select一起對錶或者檢視中的資料進行查詢並逐行讀取。

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

顯示游標(explicit cursor):在pl/sql程式中定義的、用於查詢的游標稱作顯示游標。

隱式游標(implicit cursor):是指非pl/sql程式中定義的、而且是在pl/sql中使用update/delete語句時,oracle系統自動分配的游標。

一.顯示游標

1.使用步驟

(1)定義 (2)開啟 (3)使用 (4)關閉

2.使用演示

首先建立測試用表student,指令碼如下:

create table "student" (

"stuname" varchar2(10 byte),

"stuno" varchar2(4 byte),

"age" number,

"gender" varchar2(2 char)

) (1).使用while迴圈處理游標

create or replace procedure proc_stu1 as

begin

--顯示游標使用,使用while迴圈

declare

--1.定義游標,名稱為cur_stu

cursor cur_stu is

select stuno,stuname from student order by stuno;

--定義變數,存放游標取出的資料

v_stuno varchar(4);

v_stuname varchar(20);

begin

--2.開啟游標cur_stu

open cur_stu;

--3.將游標的當前行取出存放到變數中

fetch cur_stu into v_stuno,v_stuname;

while cur_stu%found --游標所指還有資料行,則繼續迴圈

loop

--列印結果

dbms_output.put_line(v_stuno||'->'||v_stuname);

--繼續將游標所指的當前行取出放到變數中

fetch cur_stu into v_stuno,v_stuname;

end loop;

close cur_stu; --4.關閉游標

end;

end proc_stu1;

(2).使用if..else代替while迴圈處理游標

create or replace procedure proc_stu2 as

begin

--顯示游標使用,使用if判斷

declare

--1.定義游標,名稱為cur_stu

cursor cur_stu is

select stuno,stuname from student order by stuno;

--定義變數,存放游標取出的資料

v_stuno varchar(4);

v_stuname varchar(20);

begin

--2.開啟游標cur_stu

open cur_stu;

--3.將游標的當前行取出存放到變數中

fetch cur_stu into v_stuno,v_stuname;

loop

if cur_stu%found then --如果游標cur_stu所指還有資料行

--列印結果

dbms_output.put_line(v_stuno||'->'||v_stuname);

--繼續將游標所指的當前行取出放到變數中

fetch cur_stu into v_stuno,v_stuname;

else

exit;

end if;

end loop;

close cur_stu; --4.關閉游標

end;

end proc_stu2;

(3).使用for迴圈處理游標

create or replace procedure proc_stu3 as

begin

--顯示游標使用,使用for迴圈

declare

--定義游標,名稱為cur_stu

cursor cur_stu is

select stuno,stuname from student order by stuno;

begin

for stu in cur_stu

loop

dbms_output.put_line(stu.stuno||'->'||stu.stuname);

--迴圈做隱含檢查 %notfound

end loop;

--自動關閉游標

end;

end proc_stu3;

(4).常用的使用exit when處理游標

create or replace

procedure proc_stu1_1 as

begin

--顯示游標使用,使用exit when迴圈

declare

--1.定義游標,名稱為cur_stu

cursor cur_stu is

select stuno,stuname from student order by stuno;

--定義變數,存放游標取出的資料

v_stuno varchar(4);

v_stuname varchar(20);

begin

--2.開啟游標cur_stu

open cur_stu;

loop

--3.將游標的當前行取出存放到變數中

fetch cur_stu into v_stuno,v_stuname;

exit when cur_stu%notfound; --游標所指還有資料行,則繼續迴圈

--列印結果

dbms_output.put_line(v_stuno||'->'||v_stuname);

end loop;

close cur_stu; --4.關閉游標

end;

end proc_stu1_1;

二.隱式游標

1.使用演示

create or replace procedure proc_stu4 as

begin

--隱式游標使用

update student set stuname='張燕廣' where stuno='1104';

--如果更新沒有匹配則插入一條新記錄

if sql%notfound then

insert into student(stuno,stuname,age,gender)

values('1104','張燕廣',18,'男');

end if;

end proc_stu4;

2.說明

所有的sql語句在上下文區內部都是可執行的,因為都有乙個游標指向上下文區,此游標就是

sql游標,與現實游標不同的是,sql游標在pl/sql中不需要開啟和關閉,而是在執行update、

delete是自動開啟和關閉。

上面例子中就是通過sql%notfound游標屬性判斷update語句的執行結果決定是否需要插入新記錄。

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,從游標中獲...