兩種方法

目前創(chuàng)新互聯(lián)已為上1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁空間、網(wǎng)站托管運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、臨翔網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
1.聲明游標(biāo)時(shí)寫好SELECT語句,如
CURSOR r_cur1 IS select *** from tableName where 條件;
使用時(shí)
OPEN r_cur1;
LOOP
FETCH *** INTO variable;
EXIT WHEN r_cur1%NOTFOUND OR r_cur1%NOTFOUND IS NULL;
。。。
2.聲明游標(biāo)
ccc sys_refcursor;
使用時(shí)
open ccc for select dept_code,dept_name from comm.dept_dict;
loop
fetch ccc into aa,bb;
exit when ccc%notfound;
。。。
end loop;
close ccc;
一
游標(biāo)是什么
游標(biāo)字面理解就是游動(dòng)的光標(biāo)。
用數(shù)據(jù)庫語言來描述:游標(biāo)是映射在結(jié)果集中一行數(shù)據(jù)上的位置實(shí)體,有了游標(biāo),用戶就可以訪問結(jié)果集中的任意一行數(shù)據(jù)了,將游標(biāo)放置到某行后,即可對(duì)該行數(shù)據(jù)進(jìn)行操作,例如提取當(dāng)前行的數(shù)據(jù)等。
二
游標(biāo)的分類
顯式游標(biāo)和隱式游標(biāo)
顯式游標(biāo)的使用需要4步:
1.
聲明游標(biāo)
CURSOR
mycur(vartype
number)
is
select
emp_no,emp_zc
from
cus_emp_basic
where
com_no
=
vartype;
2.
打開游標(biāo)
open
mycur(000627)
注:000627是參數(shù)
3.
讀取數(shù)據(jù)
fetch
mycur
into
varno,
varprice;
4.
關(guān)閉游標(biāo)
close
mycur;
三
游標(biāo)的屬性
oracle
游標(biāo)有4個(gè)屬性:%ISOPEN,%FOUND,%NOTFOUND,%ROWCOUNT。
%ISOPEN判斷游標(biāo)是否被打開,如果打開%ISOPEN等于true,否則等于false;
%FOUND
%NOTFOUND判斷游標(biāo)所在的行是否有效,如果有效,則%FOUNDD等于true,否則等于false;
%ROWCOUNT返回當(dāng)前位置為止游標(biāo)讀取的記錄行數(shù)。
四
示例
set
serveroutput
on;
declare
varno
varchar2(20);
varprice
varchar2(20);
CURSOR
mycur(vartype
number)
is
select
emp_no,emp_zc
from
cus_emp_basic
where
com_no
=
vartype;
begin
if
mycur%isopen
=
false
then
open
mycur(000627);
end
if;
fetch
mycur
into
varno,varprice;
while
mycur%found
loop
dbms_output.put_line(varno||','||varprice);
if
mycur%rowcount=2
then
exit;
end
if;
fetch
mycur
into
varno,varprice;
end
loop;
close
mycur;
end;
PL/SQL記錄的結(jié)構(gòu)和C語言中的結(jié)構(gòu)體類似,是由一組數(shù)據(jù)項(xiàng)構(gòu)成的邏輯單元。
PL/SQL記錄并不保存在數(shù)據(jù)庫中,它與變量一樣,保存在內(nèi)存空間中,在使用記錄時(shí)候,要首先定義記錄結(jié)構(gòu),然后聲明記錄變量。可以把PL/SQL記錄看作是一個(gè)用戶自定義的數(shù)據(jù)類型。
set
serveroutput
on;
declare
type
person
is
record
(
empno
cus_emp_basic.emp_no%type,
empzc
cus_emp_basic.emp_zc%type);
person1
person;
cursor
mycur(vartype
number)is
select
emp_no,emp_zc
from
cus_emp_basic
where
com_no=vartype;
begin
if
mycur%isopen
=
false
then
open
mycur(000627);
end
if;
loop
fetch
mycur
into
person1;
exit
when
mycur%notfound;
dbms_output.put_line('雇員編號(hào):'||person1.empno||',地址:'||person1.empzc);
end
loop;
close
mycur;
end;
典型游標(biāo)for
循環(huán)
游標(biāo)for循環(huán)示顯示游標(biāo)的一種快捷使用方式,它使用for循環(huán)依次讀取結(jié)果集中的行數(shù)據(jù),當(dāng)form循環(huán)開始時(shí),游標(biāo)自動(dòng)打開(不需要open),每循環(huán)一次系統(tǒng)自動(dòng)讀取游標(biāo)當(dāng)前行的數(shù)據(jù)(不需要fetch),當(dāng)退出for循環(huán)時(shí),游標(biāo)被自動(dòng)關(guān)閉(不需要使用close)。使用游標(biāo)for循環(huán)的時(shí)候不能使用open語句,fetch語句和close語句,否則會(huì)產(chǎn)生錯(cuò)誤。
set
serveroutput
on;
declare
cursor
mycur(vartype
number)is
select
emp_no,emp_zc
from
cus_emp_basic
where
com_no=vartype;
begin
for
person
in
mycur(000627)
loop
dbms_output.put_line('雇員編號(hào):'||person.emp_no||',地址:'||person.emp_zc);
end
loop;
end;
--Oracle?PL/SQL
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;
1.游標(biāo)定義:\x0d\x0acursor XXXA is\x0d\x0a SELECT 語句;\x0d\x0aXXXB cursorName%rowtype;\x0d\x0a\x0d\x0aXXXA: 游標(biāo)名\x0d\x0aXXXB: 游標(biāo)行數(shù)據(jù)定義\x0d\x0a\x0d\x0a2. 打開游標(biāo):\x0d\x0a-- 打開之前最好先關(guān)一下,防止上次發(fā)生異常沒有關(guān)掉而引發(fā)不必要的異常\x0d\x0a IF XXXA%ISOPEN THEN\x0d\x0a CLOSE XXXA;\x0d\x0a END IF;\x0d\x0a\x0d\x0aOpen XXXA ;\x0d\x0a Loop\x0d\x0a Fetch XXXA into XXXB;\x0d\x0a exit when XXXA%NOTFOUND;\x0d\x0a... ... 處理邏輯\x0d\x0a end loop;\x0d\x0a close XXXA;
1.游標(biāo)定義:
cursor XXXA is
SELECT 語句;
XXXB cursorName%rowtype;
XXXA: 游標(biāo)名
XXXB: 游標(biāo)行數(shù)據(jù)定義
2. 打開游標(biāo):
-- 打開之前最好先關(guān)一下,防止上次發(fā)生異常沒有關(guān)掉而引發(fā)不必要的異常
IF XXXA%ISOPEN THEN
CLOSE XXXA;
END IF;
Open XXXA ;
Loop
Fetch XXXA into XXXB;
exit when XXXA%NOTFOUND;
... ... 處理邏輯
end loop;
close XXXA;
文章標(biāo)題:python中核函數(shù),核函數(shù)詳解
分享URL:http://chinadenli.net/article15/dsggedi.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、商城網(wǎng)站、網(wǎng)站收錄、自適應(yīng)網(wǎng)站、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)