oracle基礎 pl sql的游標

2021-10-02 05:26:03 字數 2362 閱讀 4774

result set ---->結果集

在pl/sql中使用游標代表乙個』集合』;

定義:cursor 游標名 [(引數名 資料型別)]

eg:

cursor c1 is select name from test

;定義了乙個游標c1,該內容是所有名稱name的集合

%found 是游標取到值為真

%notfound 是游標沒有取到值時為真

/*

游標屬性

%fount %notfount

*/declare

cursor cemp is

select name from test

; pname test.name%type;

begin

--開啟游標

open cemp;

loop

fetch cemp

into pname;

--思考 1 迴圈什麼時候退出? 2 fetch不一定能取到記錄

--exit when 沒有取到記錄;

exit when cemp%notfound;

dbms_output.put_line(

'名字是'

|| pname)

; end loop;

--關閉游標

close cemp;

end;

declare

cursor cemp is

select name, age from test

; pname test.name%type;

page test.age%type;

begin

open cemp;

loop

fetch cemp

into pname, page;

exit when cemp%notfound;

if pname =

'王思'

then

update test

set test.age = page + 1 where name = pname;

else

update test

set test.age = page + 10 where name = pname;

end if

; end loop;

close cemp;

commit;

dbms_output.put_line(

'年齡增加完成');

end;

/*

%found %notfound

%isopen 判斷游標是否開啟

%rowcount 影響的行數

*/declare

cursor cemp is select name,age from test

; pname test.name%type;

page test.age%type;

begin

open cemp;

if cemp%isopen then

dbms_output.put_line(

'游標開啟');

end if

; close cemp;

end;

alter system set open_cursors=400 scope=both;

scope取值有三個:both/memory/spfile

memory:只更改當前例項,不更改引數檔案

spfile:資料庫需要重啟,只更改引數檔案,不更改當前例項

both:二者都更改

cursor 游標名[引數名 資料型別] is select * from…

eg:查詢某個年紀的人

declare

cursor cemp(aa number) is select name,type from test where test.age=aa;

pname test.name%type;

ptype test.type%type;

begin

open cemp(33)

; loop

fetch cemp into pname,ptype;

exit when cemp%notfound;

if pname=13 then

dbms_output.put_line(ptype)

; end if

; end loop;

close cemp;

end;

oracle 中的PL SQL基礎(下)

pl sql 序列 回顧 關於序列有兩個偽列 nextval 返回序列的下乙個值 currval 返回序列的當前值。通常使用語句 select seq1 nextval from dual 來查詢序列的值。而結合pl sql我們可以採用如下方法 建立乙個序列 create sequence seq1...

Oracle基礎之PL SQL程式塊

pl sql塊由三個部分組成 說明部分 執行部分和異常處理部分。一段完整的pl sql程式塊結構如下所示 declare 說明部分 begin 塊開始標記 執行部分 exception 異常處理部分 end 塊結束標記 說明 1 說明部分 說明部分是可選的。由關鍵字declare引出,用於定義常量 ...

Oracle的pl sql程式設計

1pl sql程式設計 1.1建立過程過程 create or replace procedure 引數名 in out 引數型別 沒有長度 名字 is begin pl sql程式設計語句 end 名字 ps 最有一定要加分號和斜槓 1.2呼叫 exec 過程名 引數 call 過程名 引數名 1...