平乡企业做网站,流量精灵官网,提高seo关键词排名,动漫做那个视频网站Cursor的基本使用方法今天在用到Cursor的时候发现#xff0c;有很多游标相关的知识还是有欠缺#xff0c;在网上搜了篇基础讲解的文#xff0c;觉得还不错#xff0c;自己整理了一下发上来。虽然很基础#xff0c;但是有一些内容之前确实没有很扎实得掌握#xff0c;所以…Cursor的基本使用方法今天在用到Cursor的时候发现有很多游标相关的知识还是有欠缺在网上搜了篇基础讲解的文觉得还不错自己整理了一下发上来。虽然很基础但是有一些内容之前确实没有很扎实得掌握所以记下来也可以加深一下印象。一、Cursor的分类二、各类Cursor举例----静态游标-显式游标setserveroutputondeclarecursoremp_sor(emp_deptnoinnumber)isselect*fromempwheredeptnoemp_deptno ;emp_i emp%rowtype;begindbms_output.put_line(Getting emp from deptno 10);openemp_sor(10);loopfetchemp_sorintoemp_i;exitwhenemp_sor%notfound;dbms_output.put_line(Employee id ||emp_i.empno|| is:);dbms_output.put_line(emp_i.ename);endloop;closeemp_sor;end;/----静态游标-隐式游标-1.DMLbeginupdateempsetenameename ;--where 12;dbms_output.put_line(update ||sql%rowcount|| records);end;/----静态游标-隐式游标-2.loop forbeginforr_sorin(selectempno,enamefromemp)loopdbms_output.put_line(r_sor.empno || : || r_sor.ename);endloop;end;/----静态游标-隐式游标-3.select intodeclarevvarchar2(20);beginselectenameintovfromempwhererownum 1;dbms_output.put_line(v);dbms_output.put_line(sql%rowcount);end;/----动态游标-弱类型Declaretypercisrefcursor;cursorcisselect*fromdual;l_cursor rc;beginif(to_char(sysdate,dd) 30)thenopenl_cursorforselect * from emp;-- ref cursor with dynamic sqlelsif(to_char(sysdate,dd) 29)thenopenl_cursorforselect*fromdept;-- ref cursor with static sqlelseopenl_cursorforselect*fromdual;-- with ref cursor with static sqlendif;openc;-- the normal static cursorend;/----动态游标-强类型declaretypeemp_jobisrecord(empnonumber,enamevarchar2(20),jobvarchar2(30));typeemp_refcurisrefcursorreturnemp_job;--声明REF CURSORemp_sor emp_refcur;emp_iemp_job;beginopenemp_sorforselectempno,ename,jobfromempwhererownum 10orderby1;loopfetchemp_sorintoemp_i;exitwhenemp_sor%notfound;dbms_output.put_line(emp_i.ename ||s job is :);dbms_output.put_line(emp_i.job);endloop;closeemp_sor;end;/普通cursor与REF cursor的区别1)静态cursor不能返回到客户端只有PL/SQL才能利用它。refcursor能够被返回到客户端这就是从Oracle的存储过程返回结果集的方式。2)静态cursor可以是全局的而refcursor则不是。3)refcursor可以从子例程传递到子例程而cursor则不能。为了共享静态cursor必须在包说明或包体中把它定义为全局cursor。因为使用全局变量通常不是一种很好的编码习惯因此可以用refcursor来共享PL/SQL中的cursor无需混合使用全局变量。4)使用静态cursor通过静态SQL(但不用refcursor)比使用refcursor效率高而refcursor的使用仅限于以下几种情况1.把结果集返回给客户端2.在多个子例程之间共享cursor(实际上与上面提到的一点非常类似)3.没有其他有效的方法来达到你的目标时则使用refcursor正如必须用动态SQL时那样----动态游标-sys_refcursorDECLARETYPEmytableISTABLEOFemp%ROWTYPE;l_data mytable;l_refcsys_refcursor;BEGINOPENl_refcFORSELECTempno,ename,job,mgr,hiredate,sal,comm,deptnoFROMemp;FETCHl_refcBULKCOLLECTINTOl_data;CLOSEl_refc;FORiIN1.. l_data.COUNTLOOPDBMS_OUTPUT.put_line ( l_data(i).ename || was hired since || l_data (i).hiredate );ENDLOOP;END;非强类型的Ref cursor 和sys_refcursor的区别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.三、游标属性/*************************************************************游标属性%FOUND变量最后从游标中获取记录的时候在结果集中找到了记录。%NOTFOUND变量最后从游标中获取记录的时候在结果集中没有找到记录。%ROWCOUNT当前时刻已经从游标中获取的记录数量。%ISOPEN是否打开。**************************************************************/----静态游标-游标属性DeclareCursoremp_sorisSelect*fromempwhererownum6orderby1;emp_i emp%rowtype;numnumber:1;BeginOpenemp_sor;Fetchemp_sorintoemp_i;LoopIfemp_sor%foundthenDbms_output.put_line(Looping over record ||num|| of || emp_sor%rowcount);Fetchemp_sorintoemp_i;num : num 1;Elsifemp_sor%notfoundthenExit;---exit loop, not IFEndif;Endloop;Ifemp_sor%isopenthenCloseemp_sor;Endif;End;/