ORACLE游標的使用

2021-08-19 00:26:46 字數 3359 閱讀 5927

1、游標的說明:游標是一種向包含多條資料記錄的結果集中每次讀取一行的機制,逐行處理查詢結果,以程式設計的方式訪問資料庫。可以把游標當成指標,可以指定結果集中的任何位置,然後允許使用者對指定位置的資料進行操作。sql的游標是一種臨時資料庫物件,可以臨時存放資料表中的資料行副本,也可以指向儲存在資料表中資料行指標。

2、游標的種類及使用

(1)隱式游標:在plsql程式中執行dml sql語句(insert、update、delete、select...into...)時自動建立隱式游標,自動開啟和關閉,名字固定叫sql。隱式游標是靜態游標

可以通過隱式游標的屬性來了解操作的狀態和結果,進而控制程式的流程;sql游標名只能訪問前乙個dml操作的游標屬性,所以在dml操作完成後立即使用sql游標名來訪問游標屬性

隱式游標屬性:%notfound:sql語句影響一行或多行資料時為ture

%found:sql語句沒有影響任何行時為ture

%rowcount:返回sql語句影響的行數

%isopen:判斷游標是否開啟,隱式游標的該屬性一直是flase,因為隱式游標在執行dml sql語句時開啟,執行結束後關閉

例:begin

--insert into test(code,name) values ('aa','張三');

update test set code='bb';

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

if sql%notfound then

dbms_output.put_line('notfound為真');

else

dbms_output.put_line('notfound為假');

end if;

if sql%found then

dbms_output.put_line('found為真');

else

dbms_output.put_line('found為假');

end if;

if sql%isopen then

dbms_output.put_line('isopen為真');

else

dbms_output.put_line('isopen為假');

end if;

commit;

end;

(2)顯式游標:用於處理返回多行的查詢

無參游標語法:cursor 《游標名》 is select語句; --宣告游標

open 《游標名》; --開啟游標

fetch 《游標名》 into 《用於儲存資料的變數名》; --移動游標並獲取資料

close 《游標名》; --關閉游標

例1:無參游標,loop迴圈

declare

cursor cur is select code,name from test; --宣告游標cur

temp cur%rowtype; --宣告乙個游標變數,通配游標一行所有列資料型別

begin

execute immediate 'truncate table test_01';

open cur;--開啟游標

loop

fetch cur into temp;--將游標所在行的資料轉存到temp中

exit when cur%notfound;--當游標到最後一行時退出

insert into test_01(code,name)

values(temp.code,temp.name);

end loop;

commit;

close cur;--關閉游標

end;

例2:無參游標,while迴圈

declare

cursor cur is select code,name from test; --宣告游標

m1 varchar2(20); --宣告變數

m2 varchar2(20);

begin

execute immediate 'truncate table test_01';

open cur;--開啟游標

fetch cur into m1,m2;--將第一行資料轉存到m1,m2中!!!

while cur%found --判斷是否有資料

loop

insert into test_01(code,name) values(m1,m2);

fetch cur into m1,m2;--將下一行的資料轉存到m1,m2中!!!

end loop;

commit;

close cur;--關閉游標

end;

例3:無參游標,for迴圈

declare

cursor cur is select code,name from test; --宣告變數

begin

execute immediate 'truncate table test_01';

for temp in cur --使用for...in...可以省略開啟和關閉游標的操作

loop

insert into test_01(code,name) values(temp.code,temp.name);

end loop;

commit;

end;

通配型別操作符:

%rowtype:通配一行所有列資料型別

%type:通配某行某列資料型別

游標屬性:(游標屬性必須在關閉游標之前)

%isopen:判斷游標是否開啟

%isfound:找不到資料時

%found:找到資料時

%rowcount:返回當前游標已掃瞄的資料行數量

(3)動態游標:ref游標也叫動態游標,ref游標和游標變數用於處理動態執行的sql查詢

語法:type 3、迴圈語句:游標通常搭配迴圈語句使用

a、loop迴圈語法:

loop

exit when 表示式;

執行語句;

end loop;

b、while迴圈語法:

while 表示式;

loop

執行語句;

end loop;

c、for迴圈語法:

for 《變數》 in 《變數取值範圍(小值...大值 如:1...100)>

loop

執行語句;

end loop;

oracle游標的使用

當select語句從資料庫中返回的記錄多餘一條時,就可以使用游標 cursor 游標可以理解為一次訪問乙個的一組記錄。select語句將列提取到游標中,然後根據游標取得記錄。使用游標時需要遵從以下的5個步驟 1 宣告一些變數,用於儲存select語句返回列值 2 宣告游標,並制定select語句 3...

oracle游標的使用

游標 cursor 也稱之為游標,從字面意思理解就是游動的游標。游標是對映在結果集中一行資料上的位置實體。游標是從表中檢索出 結果集,並從中每次指向一條記錄進行互動的機制。cursor 游標名 引數名 資料型別 引數名 資料型別 is select 語句 示例 無參游標 cursor c emp i...

oracle游標的使用

游標小結 綜合使用雙層迴圈中使用游標 set serveroutput on declare cursor cdept is select deptno fromdept 10 20 30 部門的集合 dpeptno dept.deptno type 部門中員工的所有工資 cursor cemp d...