Oracle¸´Ï°±Ê¼Ç(Áù)

2015-02-02 23:25:38 ¡¤ ×÷Õß: ¡¤ ä¯ÀÀ: 38
------------------------------*/ Óα꣺ָÏò²éѯ½á¹û¼¯µÄÖ¸Õ루¶Ô²éѯ½á¹û¼¯µÄÒýÓã© Ò»¡¢ÒþʽÓαê(Àý£ºSQL%rowcount) 1¡¢Oracle×Ô¶¯ÎªÔöɾ¸Ä²Ù×÷´´½¨ÒþʽÓα꣬Ãû³ÆÍ³Ò»ÎªSQL 2¡¢ÓÃÓÚ»ñÈ¡×î½üµÄÔöɾ¸Ä²Ù×÷¶ÔÊý¾Ý¿âµÄÓ°Ï죨ÔÚÌá½»»ò»Ø¹ö֮ǰ£© 3¡¢ÓαêÊôÐÔ 1£©%rowcount ·µ»ØÊÜÓ°ÏìµÄÐÐÊý 2£©%found Èç¹ûÓ°ÏìÁËÊý¾Ý£¬Ôò·µ»Øtrue·ñÔò·µ»Øfalse 3£©%notfound Èç¹ûûӰÏìÊý¾Ý£¬Ôò·µ»Øtrue·ñÔò·µ»Øfalse 4£©%isopen Èç¹ûÓαê´ò¿ª£¬Ôò·µ»Øtrue·ñÔò·µ»Øfalse£¨ÒþʽÓαêʼÖÕ·µ»Øfalse£© ¶þ¡¢ÏÔʽÓαê(Àý£ºcursor my_cursor is select * from emp) 1¡¢ÏÔʾÓÎ±ê±ØÐëÔÚÉùÃ÷²¿·ÖÏÔʽÉùÃ÷ 2¡¢ÓÃÓÚ´¦Àí·µ»Ø¶àÐеIJéѯ½á¹û¼¯£¬±ãÓÚÓû§ÖðÐд¦ÀíÊý¾Ý 3¡¢ÓαêÊôÐÔ 1£©%rowcount ·µ»ØÓαêËùÔÚµÄÐкŠ2£©%found Èç¹ûÕÒµ½ÁËÊý¾Ý£¨fetch³É¹¦£©£¬Ôò·µ»Øtrue·ñÔò·µ»Øfalse 3£©%notfound Èç¹ûûÕÒµ½Êý¾Ý£¨fetchʧ°Ü£©£¬Ôò·µ»Øtrue·ñÔò·µ»Øfalse 4£©%isopen Èç¹ûÓαê´ò¿ª£¬Ôò·µ»Øtrue·ñÔò·µ»Øfalse Èý¡¢forÑ­»·Óα꣺¼ò»¯ÓαêµÄ²Ù×÷£¨×Ô¶¯´ò¿ª¡¢ÌáÈ¡Êý¾Ý¡¢¹Ø±Õ£© declare cursor mycursor is select * from emp; begin --'r'±íʾÓαêÖнá¹û¼¯¶ÔÓ¦µÄÐÐÀàÐͱäÁ¿ for r in mycursor loop if(r.sal>=3000) then dbms_output.put_line(mycursor%rowcount||'-'||r.ename||'-'||r.sal); end if; end loop; end; ËÄ¡¢´ø²ÎÊýµÄÓα꣺Ìá¸ßÓαêµÄÁé»îÐÔ --eg ¸ù¾Ý²¿ÃűàºÅ²éѯԱ¹¤ÐÅÏ¢ declare --ÉùÃ÷²ÎÊý£¬ÀàÐͲ»ÄÜÖ¸¶¨³¤¶È cursor mycursor(d_no number) is select * from emp where deptno=d_no; begin for r in mycursor('&d_no') --´«²Î loop dbms_output.put_line(r.ename); end loop; end; Î塢ʹÓÃÓαê¸üÐÂÊý¾Ý 1£©Ê¹ÓÃselect..for update nowait;¸øÓαê¼ÓËø 2£©Ê¹ÓÃwhere current of ÓαêÃû;ÏÞ¶¨¸üÐÂÓαêËùÔÚÐÐ --eg ¸øÔ±¹¤¼Óн£¨10+100,20+200,30+300£© declare money number; cursor mycursor is select * from emp for update nowait;--¸øÓαê¼ÓËø begin for r in mycursor loop if(r.deptno=10) then money:=100; elsif(r.deptno=20) then money:=200; elsif(r.deptno=30) then money:=300; end if; --where current of ÓαêÃû; ÏÞ¶¨¸üÐÂÓαêËùÔÚÐÐ update emp set sal=sal+money where current of mycursor; end loop; end; Áù¡¢REFÓα꣨¶¯Ì¬Óα꣩ 1¡¢ÓÃÓÚ´¦ÀíÔËÐÐʱ²ÅÄÜÈ·¶¨µÄ²éѯ½á¹û¼¯ 2¡¢REFÓÎ±ê±ØÐëÔÚÉùÃ÷²¿·Ö£º 1£©ÉùÃ÷REFÓαêÀàÐÍ£¬type ÀàÐÍÃ
û is ref cursor; 2£©ÉùÃ÷REFÓαê±äÁ¿£¬±äÁ¿Ãû ÀàÐÍÃû; 3) ¶¯Ì¬Óα겻ÄÜ´ø²ÎÊý£¿ --²éѯÿ¸öÔ±¹¤µÄÐÕÃûºÍнˮ declare type my_refcursor_type is ref cursor; my_refcursor my_refcursor_type; emp_row emp%rowtype; begin open my_refcursor for select * from emp; loop fetch my_refcursor into emp_row; exit when my_refcursor%notfound; dbms_output.put_line(emp_row.ename || ':' ||emp_row.sal); end loop; close my_refcursor; end; /*----------------------------------------´æ´¢¹ý³Ì¡¢º¯Êý¡¢¸´ºÏÀàÐÍ(record,table)¡¢°ü----------------------------------------*/ Ò»¡¢·ÖÀà 1£©¹ý³Ì procedure£¬Íê³ÉÌØ¶¨¹¦ÄÜ 2£©º¯Êý function£¬Íê³ÉÌØ¶¨¹¦Äܲ¢·µ»Ø1¸ö½á¹û 2¡¢Óŵã 1£©Ä£¿é»¯£¬°´ÒµÎñ¹¦ÄܽøÐзâ×° 2£©ÖØÓÃÐԺã¬Ò×ÓÚά»¤ 3£©Ö´ÐÐЧÂʸߣ¬¼õÉÙÍøÂçÁ÷Á¿µÄÕ¼Óà 4£©°²È«ÐԸߣ¨É漰ȨÏÞ¹ÜÀí£© ¶þ¡¢×é³É 1£©ÉùÃ÷²¿·Ö£¨±ØÐëµÄ£© create [or replace]... 2£©¿ÉÖ´Ðв¿·Ö£¨±ØÐëµÄ£© begin...end; 3£©Òì³£´¦Àí²¿·Ö£¨¿ÉÑ¡µÄ£© exception... Èý¡¢²ÎÊýģʽ 1£©ÊäÈë²ÎÊý in ½ÓÊÕÓû§ÊäÈ룬²»ÔÊÐíÔÚ¹ý³ÌÖÐÐ޸ģ¬Èç¹ûδָ¶¨²ÎÊýģʽĬÈÏΪÊäÈë²ÎÊý 2£©Êä³ö²ÎÊý out ÏòÓû§·µ»Ø½á¹û£¬±ØÐëÉùÃ÷±äÁ¿´«²Î 3£©ÊäÈëÊä³ö²ÎÊý in out ¼È½ÓÊÕÓû§ÊäÈ룬ÓÖÏòÓû§·µ»Ø½á¹û ËÄ¡¢´´½¨Óï·¨ ¹ý³Ì£º create [or replace] procedure ¹ý³ÌÃû[(²ÎÊýÁбí)] as | is [±äÁ¿Áбí;] begin ¿ÉÖ´ÐдúÂë; [exception when others then null;] end; º¯Êý£º create [or replace] function º¯ÊýÃû[(²ÎÊýÁбí)] return ·µ»ØÀàÐÍ as | is [±äÁ¿Áбí;] begin ¿ÉÖ´ÐдúÂë; return ·µ»ØÖµ; [exception when others then return null;] end; Î塢ɾ³ý×Ó³ÌÐò drop procedure ¹ý³ÌÃû; drop function º¯ÊýÃû; --¸ù¾ÝÔ±¹¤ÐÕÃû²éѯ¹¤×Ê(¹ý³Ì) create or replace procedure myproc1(e_ename in varchar2,e_sal out number) is v_sal emp.sal%type; begin select sal into e_sal from emp where ename = e_ename; end; declare sal number(20); begin myproc1('KING',sal); dbms_output.put_line('SAL:' || sal); end; --¸ù¾ÝÔ±¹¤ÐÕÃû²éѯ±àºÅ(º¯Êý) create or replace function myfun1(e_ename varchar2) return number is v_empno emp.empno%type; begin select empno into v_empno from emp where ename = e_ename; return v_empno; end; declare empno emp.empno%type; begin empno := m