北大青鳥oracle學習筆記19 20 21 22

2021-05-21 23:10:05 字數 2524 閱讀 6748

游標

指向上下文區域的控制代碼或指標

游標在伺服器段儲存

屬性 %notfound  沒有找到結果或沒有操作成功

%found  找到結果或操作成功

%rowcount  游標影響的行數

%isopen  游標是否開啟 隱式游標系統自動維護,永遠為假

型別:

靜態游標

-隱式游標

在oracle內部宣告

用於處理dml語句和返回單行的查詢 

sql為隱式游標預設名稱,例:

begin

insert

into student values('3','test','m',sysdate);

dbms_output.put_line('游標影響的行數:'||sql%rowcount);

end;

-顯式游標

由使用者顯示宣告

游標指向活動集的當前行

控制顯示游標

open 開啟游標

fetch … into …

close 關閉游標

declare

cursor stucur is

select * from student;

sturow student%rowtype;

begin

open stucur;

loop

fetch stucur into sturow;

exit

when stucur%notfound;

dbms_output.put_line(sturow.stu_name);

dbms_output.put_line(stucur%rowcount);

end loop;

close stucur;

end;

ref游標(動態游標)

在執行時使不同的語句與之關聯

ref游標使用游標變數

游標變數:一種引用型別,可以在執行時指向不同的儲存位置,close語句關閉游標並釋放用於查詢的資源。

型別:

有約束的游標變數:具有返回型別 定義游標時加上return 游標型別

declare

type refstucur is ref cursor

return stu%rowtype;

stucur refstucur;

sturow student%rowtype;

flag int :=0;

begin

flag := &flag;

if flag = 0 then

open stucur for

select * from student where stu_id <= '2';

else

open stucur for

select * from student where stu_id > '2';

endif;

loop

fetch stucur into sturow;

exit

when stucur%notfound;

dbms_output.put_line(sturow.stu_name);

end loop;

end;

無約束的游標變數:無返回型別

declare

type refstucur is ref cursor;

stucur refstucur;

sturow student%rowtype;

flag int :=0;

begin

flag := &flag;

if flag = 0 then

open stucur for

select * from student where stu_id <= '2';

else

open stucur for

select * from student where stu_id > '2';

endif;

loop

fetch stucur into sturow;

exit

when stucur%notfound;

dbms_output.put_line(sturow.stu_name);

end loop;

end;

由於在for迴圈中會自動開啟游標,無哦一ref游標不能再for迴圈中使用!

for sturow in stucur loop

… end loop;

游標限制:

不能再程式包中宣告游標變數

遠端子程式不能接受游標變數的值

不能使用比較操作符對游標變數進行相等或不相等測試

不能將空值賦予游標變數

表不能儲存游標變數的值

北大青鳥oracle學習筆記7

oracle表分割槽 oracle允許使用者對錶進行進一步規劃,即對錶進行進一步拆分,將表分成若干個邏輯部分,滅個不妨稱其為表分割槽 範圍分割槽 根據表中列值的範圍進行分割槽 語法 partition by range 欄位名 partition 分割槽名 values less than 60 6...

北大青鳥oracle學習筆記11

簇 有公共列的兩個或多個表的集合 簇表中的資料儲存在公共資料塊中 簇鍵 唯一識別符號 建立簇 減少i o操作,減少磁碟空間,但是插入效能降低。兩張表中有共同的列,比如學生表中有班級編號,班級表中也有班級編號,可以將班級編號存放在簇中 create cluster 簇名 欄位名 型別 tablespa...

北大青鳥oracle學習筆記12

關係型資料庫理論中字段值必須是單值,而oracle中允許在乙個欄位中儲存乙個表的內容。如 員工表中的外來鍵 部門編號,oracle中這個字段可以存放部門的記錄而並不是乙個外來鍵,這樣查詢時候的效率會提高。可變陣列 建立帶有可變陣列的表 建立可變陣列基型別 create or replace type...