新聞中心
如何打開和關(guān)閉Oracle游標(biāo)
--Oracle?PL/SQL
成都創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、洛江網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5技術(shù)、商城建設(shè)、集團公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為洛江等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
declare
--定義游標(biāo)
cursor?cur_test?is
select?*?from?emp;
v_emp?emp%rowtype;
begin
--打開游標(biāo)
open?cur_test;
loop
--獲取游標(biāo)值
fetch?cur_test
into?v_emp;
exit?when?cur_test%notfound;--屬性為是否提取數(shù)據(jù)成功,不成功則TRUE
dbms_output.put_line(v_emp.empno?||?'_'?||?v_emp.ename);
end?loop;
--關(guān)閉游標(biāo)
close?cur_test;
end;
Oracle 游標(biāo)
游標(biāo)能夠根據(jù)查詢條件從數(shù)據(jù)表中提取一組記錄,將其作為一個臨時表置于數(shù)據(jù)緩沖區(qū)中,利用指針逐行對記錄數(shù)據(jù)進行操作。
Oracle中的游標(biāo)分為顯示游標(biāo)和隱式游標(biāo) 。
在執(zhí)行SQL語句時,Oracle會自動創(chuàng)建隱式游標(biāo),該游標(biāo)是內(nèi)存中處理該語句的數(shù)據(jù)緩沖區(qū),存儲了執(zhí)行SQL語句的結(jié)果。通過隱式游標(biāo)屬性可獲知SQL語句的執(zhí)行狀態(tài)信息。
%found:布爾型屬性,如果sql語句至少影響到一行數(shù)據(jù),值為true,否則為false。
%notfound:布爾型屬性,與%found相反。
%rowcount:數(shù)字型屬性,返回受sql影響的行數(shù)。
%isopen:布爾型屬性,當(dāng)游標(biāo)已經(jīng)打開時返回true,游標(biāo)關(guān)閉時則為false。
用戶可以顯式定義游標(biāo)。使用顯式游標(biāo)處理數(shù)據(jù)要4個步驟:定義游標(biāo)、打開游標(biāo)、提取游標(biāo)數(shù)據(jù)和關(guān)閉游標(biāo)。
游標(biāo)由游標(biāo)名稱和游標(biāo)對應(yīng)的select結(jié)果集組成。定義游標(biāo)應(yīng)該放在pl/sql程序塊的聲明部分。
語法格式:cursor 游標(biāo)名稱(參數(shù)) is 查詢語句
打開游標(biāo)時,游標(biāo)會將符合條件的記錄送入數(shù)據(jù)緩沖區(qū),并將指針指向第一條記錄。
語法格式:open 游標(biāo)名稱(參數(shù));
將游標(biāo)中的當(dāng)前行數(shù)據(jù)賦給指定的變量或記錄變量。
語法格式:fetch 游標(biāo)名稱 into 變量名;
游標(biāo)一旦使用完畢,就應(yīng)將其關(guān)閉,釋放與游標(biāo)相關(guān)聯(lián)的資源。
語法格式:close 游標(biāo)名稱;
declare
cursor c1 is? select sno,cno,grade from sc;
v_sno sc.sno%type;
v_cno sc.cno%type;
v_grade sc.grade%type;
begin
open c1;
loop
? fetch c1 into v_sno,v_cno,v_grade;
? exit when c1%notfound;--緊跟fetch之后
if c1%found then
dbms_output.put_line(to_char(c1%rowcount)||v_cno);
end if;
end loop;
close c1;?
end;
declare
cursor c1 is select sno,cno,grade from sc;
v_sno sc.sno%type;
v_cno sc.cno%type;
v_grade sc.grade%type;
begin
open c1;
fetch c1 into v_sno,v_cno,v_grade;
while c1%found loop
? dbms_output.put_line(v_sno||v_cno||v_grade);
?fetch c1 into v_sno,v_cno,v_grade;
end loop;
close c1;?
end;
第三種:for
declare
cursor c1 is select sno,cno,grade from sc;
begin
for item in c1 loop
dbms_output.put_line(rpad(item.sno,'10',' ')||rpad(item.cno,'10',' ')||rpad(item.grade,'10',' '));
end loop;
end;
Oracle游標(biāo)sql語句代碼塊的優(yōu)化
游標(biāo)操作的優(yōu)化:
-- 有一個表格,存儲的是用戶的名字,將 emp 表中所有的用戶名轉(zhuǎn)換成小寫,再寫入這個表格
create table ename_emp(
ename varchar2(50)
);
-- 下面這個游標(biāo)的操作,需要 43s時間才能完成
declare
begin
for i in (select ename from emp_liebiao) loop
insert into ename_emp values(lower(i.ename));
commit;
end loop;
end;
-- 因為游標(biāo)是以行為單位進行數(shù)據(jù)的操作的,所有游標(biāo)的效率是比較慢的,而且游標(biāo)需要消耗的內(nèi)存也是比較多的,我們需要將游標(biāo)以行進行操作的方式,修改成一次性操作所有數(shù)據(jù)的批量的方式。
批量操作在游標(biāo)里面 叫做 bulk collect
第一步,創(chuàng)建一個表類型
type 表類型的名字 is table of 表名.列名 %type;
變量名 表類型的名字;
第三步,創(chuàng)建一個游標(biāo),讀取某個查詢的結(jié)果
cursor 游標(biāo)名字 is select 查詢語句 ;
第四步,打開游標(biāo)
open 游標(biāo)名字;
第五步,捕獲游標(biāo)的數(shù)據(jù),將內(nèi)容給到表類型的變量進行保存
fetch 游標(biāo)名字 bulk collect into 變量名字 ;
第六步,使用 forall 語句,對數(shù)據(jù)進行批量的操作
forall i in 變量 .first .. 變量 .last DML 語句操作 ;
第七步,關(guān)閉游標(biāo)
close 游標(biāo)名字 ;
declare
-- 創(chuàng)建表類型
type biao is table of emp_liebiao.ename%type;
b biao;
-- 創(chuàng)建游標(biāo)
cursor m is select ename from emp_liebiao;
begin
-- 打開游標(biāo)
open m;
-- 將游標(biāo)的內(nèi)容批量的給到變量
fetch m bulk collect into b;
-- 使用forall批量修改數(shù)據(jù)
forall i in b.first .. b.last insert into ename_emp values(lower(b(i)));
commit;
-- 關(guān)閉游標(biāo)
close m;
end;
練習(xí):批量修改用戶的編號,讓編號+工資等級+部門,形成一個新的編號,存入下面的表格中,使用 bulk collect 來實現(xiàn)。
2000以下是 C ,2000-3000是 B ,3000以上是 A ,
例如 SMITH , 新編號應(yīng)該是 7369_C_20
create table empno_emp(
empno varchar2(50)
);
declare
type biao is table of emp_liebiao%rowtype;
b biao;
cursor m is select * from emp_liebiao;
begin
open m;
fetch m bulk collect into b;
forall i in b.first..b.last
insert into empno_emp values(
b(i).empno||'_'||decode(sign(b(i).sal-2000)+sign(b(i).sal-3000),-2,'C',2,'A','B')||'_'||b(i).deptno
);
commit;
close m;
end;
oracle如何關(guān)閉游標(biāo)?
1. 用open打開的,用close關(guān)閉\x0d\x0adeclare\x0d\x0acursor mycursor is\x0d\x0aselect * from emp for update;\x0d\x0amyrecord emp%rowtype;\x0d\x0abegin\x0d\x0aopen mycursor;\x0d\x0aloop\x0d\x0afetch mycursor into myrecord;\x0d\x0aexit when mycursor%notfound;\x0d\x0aif (myrecord.sal=2000) then\x0d\x0aupdate emp\x0d\x0aset sal=2001\x0d\x0awhere current of mycursor;\x0d\x0aend if;\x0d\x0aend loop;\x0d\x0aclose mycursor;\x0d\x0acommit;\x0d\x0aend;\x0d\x0a2. 用for 循環(huán)的,循環(huán)完了就自己關(guān)了\x0d\x0adeclare\x0d\x0acursor mycursor is\x0d\x0aselect * from emp;\x0d\x0abegin\x0d\x0afor i in mycursor\x0d\x0aloop\x0d\x0adbms_output.put_line(i.job);\x0d\x0aend loop;\x0d\x0aend;
當(dāng)前文章:怎么關(guān)閉oracle游標(biāo) oracle查詢打開的游標(biāo)
網(wǎng)站地址:http://ef60e0e.cn/article/hisihg.html