<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Decode360's Blog

    業(yè)精于勤而荒于嬉 QQ:150355677 MSN:decode360@hotmail.com

      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 ::  :: 管理 ::
      397 隨筆 :: 33 文章 :: 29 評(píng)論 :: 0 Trackbacks
    Cursor的基本使用方法

    ?

    ??? 今天在用到Cursor的時(shí)候發(fā)現(xiàn),有很多游標(biāo)相關(guān)的知識(shí)還是有欠缺,在網(wǎng)上搜了篇基礎(chǔ)講解的文,覺(jué)得還不錯(cuò),自己整理了一下發(fā)上來(lái)。雖然很基礎(chǔ),但是有一些內(nèi)容之前確實(shí)沒(méi)有很扎實(shí)得掌握,所以記下來(lái)也可以加深一下印象。

    ?

    一、Cursor的分類(lèi)

    ?

    ??? Cursor

    ?

    ?

    二、各類(lèi)Cursor舉例

    ?

    ---- 靜態(tài)游標(biāo) - 顯式游標(biāo)

    set serveroutput on

    declare

    ? cursor emp_sor(emp_deptno in number ) is

    ??? select * from emp where deptno=emp_deptno ;

    ? emp_i emp% rowtype ;

    begin

    ? dbms_output.put_line( 'Getting emp from deptno 10' );

    ? open emp_sor( 10 );

    ? loop

    ??? fetch emp_sor into emp_i;

    ??? exit when emp_sor% notfound ;

    ??? dbms_output.put_line( 'Employee id ' ||emp_i.empno|| ' is:' );

    ??? dbms_output.put_line(emp_i.ename);

    ? end loop ;

    ? close emp_sor;

    end ;

    /

    ?

    ---- 靜態(tài)游標(biāo) - 隱式游標(biāo) -1.DML

    begin

    ? update emp set ename=ename ; --where 1=2;

    ?dbms_output.put_line( 'update ' || sql % rowcount || ' records' );

    end ;

    /

    ?

    ---- 靜態(tài)游標(biāo) - 隱式游標(biāo) -2.loop for

    begin

    ? for r_sor in ( select empno,ename from emp)

    ? loop

    ?? dbms_output.put_line(r_sor.empno || ' : ' || r_sor.ename);

    ? end loop ;

    end ;

    /

    ?

    ---- 靜態(tài)游標(biāo) - 隱式游標(biāo) -3.select into

    declare

    ? v? varchar2 ( 20 );

    begin

    ? select ename into v from emp

    ? where rownum = 1 ;

    ? dbms_output.put_line(v);

    ? dbms_output.put_line( sql % rowcount );

    end ;

    /

    ?

    ---- 動(dòng)態(tài)游標(biāo) - 弱類(lèi)型

    Declare

    ? type rc is ref cursor ;

    ? cursor c is select * from dual;

    ? l_cursor rc;

    begin

    ? if (to_char( sysdate , 'dd' ) = 30 ) then

    ???? open l_cursor for 'select * from emp' ;-- ref cursor with dynamic sql

    ? elsif (to_char( sysdate , 'dd' ) = 29 ) then

    ???? open l_cursor for select * from dept;-- ref cursor with static sql

    ? else

    ???? open l_cursor for select * from dual;-- with ref cursor with static sql

    ? end if ;

    ? open c;-- the "normal" static cursor

    end ;

    /

    ?

    ---- 動(dòng)態(tài)游標(biāo) - 強(qiáng)類(lèi)型

    declare

    ? type emp_job is record (empno number ,

    ???????????????????????? ename varchar2 ( 20 ),

    ???????????????????????? job?? varchar2 ( 30 )

    ???????????????????????? );

    ? type emp_refcur is ref cursor return emp_job; -- 聲明 REF CURSOR

    ? emp_sor emp_refcur;

    ? emp_i?? emp_job;

    begin

    ? open emp_sor for

    ??? select empno,ename,job from emp where rownum < 10 order by 1 ;

    ? loop

    ??? fetch emp_sor into emp_i;

    ? exit when emp_sor% notfound ;

    ??? dbms_output.put_line(emp_i.ename || '''s job is :' );

    ??? dbms_output.put_line(emp_i.job);

    ? end loop ;

    ? close emp_sor;

    end ;

    /

    ?

    ?

    普通cursor與REF cursor的區(qū)別:

    ?

    1)靜態(tài)cursor不能返回到客戶(hù)端,只有PL/SQL才能利用它。ref cursor能夠被返回到客戶(hù)端,這就是從Oracle的存儲(chǔ)過(guò)程返回結(jié)果集的方式。

    2)靜態(tài)cursor可以是全局的,而ref cursor則不是。

    3)ref cursor可以從子例程傳遞到子例程,而cursor則不能。為了共享靜態(tài)cursor,必須在包說(shuō)明或包體中把它定義為全局cursor。

    ?? 因?yàn)槭褂萌肿兞客ǔ2皇且环N很好的編碼習(xí)慣,因此可以用ref cursor來(lái)共享PL/SQL中的cursor,無(wú)需混合使用全局變量。

    4)使用靜態(tài)cursor,通過(guò)靜態(tài)SQL(但不用ref cursor),比使用ref cursor效率高,而ref cursor的使用僅限于以下幾種情況:

    ??? 1.把結(jié)果集返回給客戶(hù)端;
    ??? 2.在多個(gè)子例程之間共享cursor(實(shí)際上與上面提到的一點(diǎn)非常類(lèi)似);
    ??? 3.沒(méi)有其他有效的方法來(lái)達(dá)到你的目標(biāo)時(shí),則使用ref cursor,正如必須用動(dòng)態(tài)SQL時(shí)那樣

    ?

    ---- 動(dòng)態(tài)游標(biāo) -sys_refcursor

    DECLARE

    ? TYPE mytable IS TABLE OF emp% ROWTYPE ;

    ? l_data mytable;

    ? l_refc sys_refcursor ;

    BEGIN

    ? OPEN l_refc FOR

    ? SELECT empno,ename,job,mgr,hiredate,sal,comm,deptno FROM emp;

    ? FETCH l_refc BULK COLLECT INTO l_data;

    ? CLOSE l_refc;

    ? FOR i IN 1 .. l_data.COUNT

    ? LOOP

    ??? DBMS_OUTPUT.put_line ( l_data(i).ename || ' was hired since ' || l_data (i).hiredate );

    ? END LOOP ;

    END ;

    ?

    非強(qiáng)類(lèi)型的Ref cursor 和sys_refcursor的區(qū)別:

    A REF CURSOR that does not specify the return type such as SYS_REFCURSOR. Meaning the SYS_REFCURSOR can be opened for a dynamic SQL query, where as simple REF CURSOR can not be opened for a query dynamically built at execution time.

    ?

    ?

    三、游標(biāo)屬性

    ?

    /*************************************************************

    游標(biāo)屬性:

    %FOUND :變量最后從游標(biāo)中獲取記錄的時(shí)候,在結(jié)果集中找到了記錄。

    %NOTFOUND :變量最后從游標(biāo)中獲取記錄的時(shí)候,在結(jié)果集中沒(méi)有找到記錄。

    %ROWCOUNT :當(dāng)前時(shí)刻已經(jīng)從游標(biāo)中獲取的記錄數(shù)量。

    %ISOPEN :是否打開(kāi)。

    **************************************************************/

    ?

    ---- 靜態(tài)游標(biāo) - 游標(biāo)屬性

    Declare

    ? Cursor emp_sor is

    ? Select * from emp where rownum< 6 order by 1 ;

    ? emp_i emp% rowtype ;

    ? num number := 1 ;

    Begin

    ? Open emp_sor;

    ? Fetch emp_sor into emp_i;

    ? Loop

    ??? If emp_sor% found then

    ????? Dbms_output.put_line( 'Looping over record ' ||num|| ' of ' || emp_sor% rowcount );

    ????? Fetch emp_sor into emp_i;

    ? ???? num := num + 1 ;

    ??? Elsif emp_sor% notfound then

    ????? Exit ;? ---exit loop, not IF

    ??? End if ;

    ? End loop ;

    ?

    ? If emp_sor% isopen then

    ??? Close emp_sor;

    ? End if ;

    End ;

    /

    ?

    posted on 2008-08-29 22:43 decode360 閱讀(473) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): 06.PLSQL
    主站蜘蛛池模板: 99re在线视频免费观看| 亚洲网红精品大秀在线观看| 一个人看的免费观看日本视频www 一个人看的免费视频www在线高清动漫 | 国产亚洲精品美女久久久久 | 午夜在线a亚洲v天堂网2019| 91免费国产在线观看| 亚洲欧洲春色校园另类小说| 2021在线永久免费视频| 精品日韩亚洲AV无码一区二区三区| 日韩精品免费在线视频| 亚洲国产一区国产亚洲| 91免费国产精品| 亚洲国产精品综合一区在线| 国产香蕉免费精品视频| 在线综合亚洲中文精品| 成人毛片视频免费网站观看| 亚洲精品无AMM毛片| 国产成人青青热久免费精品| 四虎影视久久久免费| 国产亚洲一区二区三区在线不卡| 三级黄色免费观看| 久久精品亚洲视频| 8x网站免费入口在线观看| 亚洲国产视频一区| 成人免费在线视频| 小说区亚洲自拍另类| 亚洲精品国产电影| 日韩av无码免费播放| 亚洲色欲或者高潮影院| 欧美a级成人网站免费| 亚洲av无码成人影院一区| 亚洲 自拍 另类小说综合图区| jizz在线免费观看| 亚洲国产精品热久久| 欧美a级在线现免费观看| 国产亚洲精品成人久久网站| 亚洲精品国产精品乱码不99| 18观看免费永久视频| 亚洲欧美日韩久久精品| 亚洲精品网站在线观看不卡无广告| 99精品视频在线观看免费|