oracle cusor 定义
定義
?1.type cursorType is ref cursor;
2.procedure getApolloPortolioAllPortfolios(
??? p_portfolios out sys_refcursor
? );
http://orajc.blog.51cto.com/458434/105931
?
游標(biāo)是數(shù)據(jù)庫中一個命名的工作區(qū),當(dāng)游標(biāo)被聲明后,他就與一個固定的SQL想關(guān)聯(lián),在編譯時(shí)刻是已知的,是靜態(tài)的.它永遠(yuǎn)指向一個相同的查詢工作區(qū).
游標(biāo)變量可以在運(yùn)行時(shí)刻與不同的SQL語句關(guān)聯(lián),在運(yùn)行時(shí)可以取不同的SQL語句.它可以引用不同的工作區(qū).
游標(biāo)和游標(biāo)變量是不能相互代替的. 如何定義游標(biāo)類型
TYPE ref_type_name IS REF CURSOR [RETURN return_type];
聲明游標(biāo)變量
cursor_name ref_type_name; ? ref_type_name 是后面聲明游標(biāo)變量時(shí)要用到的我們的游標(biāo)類型(自定義游標(biāo)類型,即CURSOR是系統(tǒng)默認(rèn)的,ref_type_name是我們定義的 );return_type代表數(shù)據(jù)庫表中的一行,或一個記錄類型
TYPE ref_type_name IS REF CURSOR RETURN employee%TYPE RETURN 是可選的,如果有是強(qiáng)類型,可以減少錯誤,如果沒有return是弱引用,有較好的靈活性. ? 不能在包頭里面聲明游標(biāo)變量,注意,但可以定義游標(biāo)類型,要注意這二者的區(qū)別. 可以聲明游標(biāo)變量作為函數(shù)或過程的形式參數(shù).
%TYPE一個列類型
%ROWTYPE行類型 控制游標(biāo)變量
OPEN-FOR(打開游標(biāo)變量,與多行查詢連接起來) FETCH(從結(jié)果集中取行數(shù)據(jù)),close(關(guān)閉游標(biāo)變量) BULK COLLECT子句將游標(biāo)變量中的行一次性提取到一個集合中. ? 游標(biāo)遍歷: 1.
declare
cursor emp_cur ( p_deptid in number) is
select * from employees where department_id = p_deptid;
open emp_cur(30);
loop
? fetch emp_cur into l_emp;
? exit when emp_cur%notfound;
? dbms_output.put_line('Employee id '|| l_emp.employee_id || ' is ');
? dbms_output.put_line(l_emp.first_name || ' ' || l_emp.last_name);
end loop;
close emp_cur;
?
2.for rec_ptrs in (select * from pvtrs order by pvtrsid asc)
?loop
??dbms_output.put_line('pvtrsid:'||rec_ptrs.pvtrsid);
??--feedOasys
??begin
???select count(*) into vcount from REPORTS_PORTFOLIOS_MAPPING where REPORTID=1 and PVTRSID=rec_ptrs.pvtrsid;
???if (vcount > 0) then
????vfeedOasys := 'Y';
???else
????vfeedOasys := 'N';
???end if;
???if (vfeedOasys = 'Y') then
????--feed config
????vMTM := 'N';
????vIA := 'N';
????vnotionalComitment := 'N';
????begin
?????select MTMFEED,IAFEED,NCFEED into feedRec from OASYS_FEEDS_PORTFOLIOS_CONFIG where PVTRSID=rec_ptrs.pvtrsid;
?????if (feedRec.MTMFEED = 'Y') then
??????vMTM := 'Y';
?????end if;
?????if (feedRec.IAFEED = 'Y') then
??????vIA := 'Y';
?????end if;
?????if (feedRec.NCFEED = 'Y') then
??????vnotionalComitment := 'Y';
?????end if;
????exception
?????when NO_DATA_FOUND then
??? ?????vMTM := 'N';
??????vIA := 'N';
??????vnotionalComitment := 'N';
????end;
????dbms_output.put_line('vMTM,vIA,vnotionalComitment:'||vMTM||','||vIA||','||vnotionalComitment);
???else
????vMTM := 'N';
????vIA := 'N';
????vnotionalComitment := 'N';
???end if;
??exception
??? ??when NO_DATA_FOUND then
??? ???vfeedOasys := 'N';
??end;
??dbms_output.put_line('vfeedOasys:'||vfeedOasys);??
?end loop;
?
3.?open p_data for
???? select ASOFDATE, ISSUERNAME, NOTIONALAMOUNTPCCY, WAGCURRENTPRICE,
??????????? WAGCURRENTPRICE - nvl(findIssuerDatePrice(dayBefore, ISSUERNAME),nvl(findIssuerDatePrice(dayBeforeminusone, ISSUERNAME), nvl(findIssuerDatePrice(dayBeforeminustwo, ISSUERNAME), findIssuerDatePrice(dayBeforeminusthree, ISSUERNAME))))??? DailyChange,
??????????? WAGCURRENTPRICE - nvl(findIssuerDatePrice(weekBefore, ISSUERNAME),nvl(findIssuerDatePrice(weekBeforeminusone, ISSUERNAME), nvl(findIssuerDatePrice(weekBeforeminustwo, ISSUERNAME),findIssuerDatePrice(weekBeforeminusthree, ISSUERNAME)))) WeeklyChange,
??????????? WAGCURRENTPRICE - nvl(findIssuerDatePrice(monthBefore, ISSUERNAME),nvl(findIssuerDatePrice(monthBeforeminusone, ISSUERNAME), nvl(findIssuerDatePrice(monthBeforeminustwo, ISSUERNAME), findIssuerDatePrice(monthBeforeminusthree, ISSUERNAME)))) MonthlyChange
???? from (
????????? select ASOFDATE, ISSUERNAME, NOTIONALAMOUNTPCCY, WAGCURRENTPRICE
????????? from Rpt_IssuerNotionalPriceHIST
????????? where ASOFDATE = closeDate
????????? order by NOTIONALAMOUNTPCCY desc, ISSUERNAME
????????? )
??? where ROWNUM <= 10;
Oracle Cursors語法總結(jié)
關(guān)于Oracle Cursors的語法總結(jié),Oracle Cursors是用來查詢數(shù)據(jù)庫,獲取記錄集合(結(jié)果集)的指針,可以讓開發(fā)者一次訪問一行結(jié)果集,在每條結(jié)果集上作操作。
一.Oracle的Cursors概念:
游標(biāo):用來查詢數(shù)據(jù)庫,獲取記錄集合(結(jié)果集)的指針,可以讓開發(fā)者一次訪問一行結(jié)果集,在每條結(jié)果集上作操作。
?
二.Oracle的Cursors分類:
1.靜態(tài)游標(biāo):
分為顯式游標(biāo)和隱式游標(biāo)。
2.REF游標(biāo):
是一種引用類型,類似于指針。
?
三.Oracle的Cursors詳細(xì)內(nèi)容:
1.顯式游標(biāo):
CURSOR游標(biāo)名(參數(shù)) [返回值類型] IS
Select語句
生命周期:
a.打開游標(biāo)(OPEN)
解析,綁定。。。不會從數(shù)據(jù)庫檢索數(shù)據(jù)
b.從游標(biāo)中獲取記錄(FETCH INTO)
執(zhí)行查詢,返回結(jié)果集。通常定義局域變量作為從游標(biāo)獲取數(shù)據(jù)的緩沖區(qū)。
c.關(guān)閉游標(biāo)(CLOSE)
完成游標(biāo)處理,用戶不能從游標(biāo)中獲取行。還可以重新打開。
選項(xiàng):參數(shù)和返回類型
set serveroutput on
declare
cursor emp_cur ( p_deptid in number) is
select * from employees where department_id = p_deptid;
l_emp employees%rowtype;
begin
dbms_output.put_line('Getting employees from department 30');
open emp_cur(30);
loop
? fetch emp_cur into l_emp;
? exit when emp_cur%notfound;
? dbms_output.put_line('Employee id '|| l_emp.employee_id || ' is ');
? dbms_output.put_line(l_emp.first_name || ' ' || l_emp.last_name);
end loop;
close emp_cur;
dbms_output.put_line('Getting employees from department 90');
open emp_cur(90);
loop
? fetch emp_cur into l_emp;
? exit when emp_cur%notfound;
? dbms_output.put_line('Employee id '|| l_emp.employee_id || ' is ');
? dbms_output.put_line(l_emp.first_name || ' ' || l_emp.last_name);
end loop;
close emp_cur;
end;
/
?
2.隱式游標(biāo):
不用明確建立游標(biāo)變量,分兩種:
a.在PL/SQL中使用DML語言,使用ORACLE提供的名為SQL的隱示游標(biāo)
b.CURSOR FOR LOOP,用于for loop語句
a.舉例:
declare
begin
update departments set department_name=department_name;
--where 1=2;
?
dbms_output.put_line('update '|| sql%rowcount ||' records');
end;
/
?
b.舉例:
declare
begin
for my_dept_rec in ( select department_name, department_id from departments)
loop
? dbms_output.put_line(my_dept_rec.department_id || ' : ' || my_dept_rec.department_name);
end loop;
end;
/
?
c.舉例:
單獨(dú)select
declare
l_empno emp.EMPLOYEE_ID%type;
-- l_ename emp.ename%type;
begin
select EMPLOYEE_ID??
? into l_empno
from emp;
--where rownum =1;
dbms_output.put_line(l_empno);
end;
/
?
使用INTO獲取值,只能返回一行。
?
游標(biāo)屬性:
%FOUND:變量最后從游標(biāo)中獲取記錄的時(shí)候,在結(jié)果集中找到了記錄。
%NOTFOUND:變量最后從游標(biāo)中獲取記錄的時(shí)候,在結(jié)果集中沒有找到記錄。
%ROWCOUNT:當(dāng)前時(shí)刻已經(jīng)從游標(biāo)中獲取的記錄數(shù)量。
%ISOPEN:是否打開。
Declare
Cursor emps is
Select * from employees where rownum<6 order by 1;
?
Emp employees%rowtype;
Row number :=1;
Begin
Open emps;
Fetch emps into emp;
?
Loop
? If emps%found then
?? Dbms_output.put_line('Looping over record '||row|| ' of ' || emps%rowcount);
?? Fetch emps into emp;
?? Row := row + 1;
? Elsif emps%notfound then
?? Exit;? ---exit loop, not IF
? End if;
End loop;
?
If emps%isopen then
? Close emps;
End if;
End;
/
?
顯式和隱式游標(biāo)的區(qū)別:
盡量使用隱式游標(biāo),避免編寫附加的游標(biāo)控制代碼(聲明,打開,獲取,關(guān)閉),也不需要聲明變量來保存從游標(biāo)中獲取的數(shù)據(jù)。
?
3.REF CURSOR游標(biāo):
動態(tài)游標(biāo),在運(yùn)行的時(shí)候才能確定游標(biāo)使用的查詢。分類:
1.強(qiáng)類型(限制)REF CURSOR,規(guī)定返回類型
2.弱類型(非限制)REF CURSOR,不規(guī)定返回類型,可以獲取任何結(jié)果集。
TYPE ref_cursor_name IS REF CURSOR [RETURN return_type]
?
Declare
Type refcur_t is ref cursor;
?
Type emp_refcur_t is ref cursor return employee%rowtype;
Begin
Null;
End;
/
?
強(qiáng)類型舉例:
declare
--聲明記錄類型
type emp_job_rec is record(
? employee_id number,
? employee_name varchar2(50),
? job_title varchar2(30)
);
--聲明REF CURSOR,返回值為該記錄類型
type emp_job_refcur_type is ref cursor
? return emp_job_rec;
--定義REF CURSOR游標(biāo)的變量
emp_refcur emp_job_refcur_type;
emp_job emp_job_rec;
begin
open emp_refcur for
? select e.employee_id,
??? e.first_name || ' ' ||e.last_name "employee_name",
??? j.job_title
? from employees e, jobs j
? where e.job_id = j.job_id and rownum < 11 order by 1;
fetch emp_refcur into emp_job;
while emp_refcur%found loop
? dbms_output.put_line(emp_job.employee_name || '''s job is ');
? dbms_output.put_line(emp_job.job_title);
? fetch emp_refcur into emp_job;
end loop;
end;
/
?
本篇文章來源于 PHP資訊 原文鏈接:http://www.phpq.net/oracle/oracle-cursors-syntax.html
=================
屬性
http://www.cnblogs.com/zndavid/archive/2009/06/09/1499601.html
當(dāng)執(zhí)行一條DML語句后,DML語句的結(jié)果保存在四個游標(biāo)屬性中,這些屬性用于控制程序流程或者了解程序的狀態(tài)。當(dāng)運(yùn)行DML語句時(shí),PL/SQL打開一個內(nèi)建游標(biāo)并處理結(jié)果,游標(biāo)是維護(hù)查詢結(jié)果的內(nèi)存中的一個區(qū)域,游標(biāo)在運(yùn)行DML語句時(shí)打開,完成后關(guān)閉。隱式游標(biāo)只使用SQL%FOUND,SQL%NOTFOUND,SQL%ROWCOUNT三個屬性.SQL%FOUND,SQL%NOTFOUND是布爾值,SQL%ROWCOUNT是整數(shù)值。SQL%FOUND和SQL%NOTFOUND
在執(zhí)行任何DML語句前SQL%FOUND和SQL%NOTFOUND的值都是NULL,在執(zhí)行DML語句后,SQL%FOUND的屬性值將是:
. TRUE :INSERT
. TRUE :DELETE和UPDATE,至少有一行被DELETE或UPDATE.
. TRUE :SELECT INTO至少返回一行
當(dāng)SQL%FOUND為TRUE時(shí),SQL%NOTFOUND為FALSE。
SQL%ROWCOUNT
在執(zhí)行任何DML語句之前,SQL%ROWCOUNT的值都是NULL,對于SELECT INTO語句,如果執(zhí)行成功,SQL%ROWCOUNT的值為1,如果沒有成功,SQL%ROWCOUNT的值為0,同時(shí)產(chǎn)生一個異常NO_DATA_FOUND.
SQL%ISOPEN
SQL%ISOPEN是一個布爾值,如果游標(biāo)打開,則為TRUE, 如果游標(biāo)關(guān)閉,則為FALSE.對于隱式游標(biāo)而言SQL%ISOPEN總是FALSE,這是因?yàn)殡[式游標(biāo)在DML語句執(zhí)行時(shí)打開,結(jié)束時(shí)就立即關(guān)閉。
?
總結(jié)
以上是生活随笔為你收集整理的oracle cusor 定义的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle 数组定义
- 下一篇: 笔记本BIOS