PL/SQL中cursor(光标/游标)的用法

来源:互联网 发布:微信水果老虎机源码 编辑:程序博客网 时间:2024/06/05 11:13

显式cursor

显式是相对与隐式cursor而言的,就是有一个明确的声明的cursor.显式游标的声明类似如下(详细的语法参加plsql ref doc )

cursor cursor_name (parameter list) is select ...

游标从declare、open、fetch、close是一个完整的生命旅程。当然了一个这样的游标是可以被多次open进行使用的,显式cursor是静态cursor,她的作用域是全局的,但也必须明白,静态cursor也只有pl/sql代码才可以使用她。下面看一个简单的静态显式cursor的示例:

declare            cursor get_gsmno_cur (p_nettype in varchar2) is                select gsmno                  from gsm_resource                  where nettype=p_nettype and status='0';             v_gsmno gsm_resource.gsmno%type;          begin             open get_gsmno_cur('139');             loop                  fetch get_gsmno_cur into v_gsmno;                  exit when get_gsmno_cur%notfound;                          dbms_output.put_line(v_gsmno);             end loop;             close emp_cur;                          open get_gsmno_cur('138');             loop                  fetch get_gsmno_cur into v_gsmno;                  exit when get_gsmno_cur%notfound;                          dbms_output.put_line(v_gsmno);             end loop;             close get_gsmno_cur;          end;          /  

上面这段匿名块用来实现选号的功能,我们显式的定义了一个get_gsmno_cur,然后根据不同的号段输出当前系统中该号短对应的可用手机号码。当然了,实际应用中没人这么用的,我只是用来说应一个显式cursor的用法。

隐式cursor

隐式cursor当然是相对于显式而言的,就是没有明确的cursor的declare。在Oracle的PL/SQL中,所有的DML操作都被Oracle内部解析为一个cursor名为SQL 的隐式游标,只是对我们透明罢了。

另外,我们前面提到的一些循环操作中的指针for 循环,都是隐式cursor.

 

隐式cursor示例一:

CREATE TABLE zrp (str VARCHAR2(10));          insert into zrp values ('ABCDEFG');          insert into zrp values ('ABCXEFG');          insert into zrp values ('ABCYEFG');          insert into zrp values ('ABCDEFG');          insert into zrp values ('ABCZEFG');          COMMIT;                    SQL> begin            2    update zrp SET str = 'updateD' where str like '%D%';            3    ifSQL%ROWCOUNT  = 0 then            4      insert into zrp values ('1111111');            5    end if;            6  end;            7  /                    PL/SQL procedure successfully completed                    SQL> select * from zrp;                    STR          ----------          updateD          ABCXEFG          ABCYEFG          updateD          ABCZEFG                    SQL>           SQL> begin            2    update zrp SET str = 'updateD' where str like '%S%';            3    ifSQL%ROWCOUNT  = 0 THEN            4      insert into zrp values ('0000000');            5    end if;            6  end;            7  /                    PL/SQL procedure successfully completed                    SQL> select * from zrp;                    STR          ----------          updateD          ABCXEFG          ABCYEFG          updateD          ABCZEFG0000000  6 rows selected                    SQL>  

隐式cursor示例二:

begin            for rec in (select gsmno,status from gsm_resource) loop                dbms_output.put_line(rec.gsmno||'--'||rec.status);            end loop;          end;          /  

REF cursor

Ref cursor属于动态cursor(直到运行时才知道这条查询).

从技术上讲,在最基本的层次静态cursor和ref cursor是相同的。一个典型的PL/SQL光标按定义是静态的。Ref光标正好相反,可以动态地打开,或者利用一组SQL静态语句来打开,选择哪种方法由逻辑确定(一个IF/THEN/ELSE代码块将打开一个或其它的查询)。例如,下面的代码块显示一个典型的静态SQL光标,光标C。此外,还显示了如何通过使用动态SQL或静态SQL来用ref光标(在本例中为L_CURSOR)来打开一个查询:

Declare        type rc is ref cursor;        cursor c is select * from dual;                l_cursor rc;      begin        if (to_char(sysdate,'dd') = 30) then            -- ref cursor with dynamic sql            open l_cursor for 'select * from emp';        elsif (to_char(sysdate,'dd') = 29) then            -- ref cursor with static sql            open l_cursor for select * from dept;        else             -- with ref cursor with static sql             open l_cursor for select * from dual;        end if;        -- the "normal" static cursor         open c;      end;      /  

在这段代码块中,可以看到了最显而易见的区别:无论运行多少次该代码块,光标C总是select * from dual。相反,ref光标可以是任何结果集,因为"select * from emp"字符串可以用实际上包含任何查询的变量来代替。

在上面的代码中,声明了一个弱类型的REF cursor,下面再看一个强类型(受限)的REF cursor,这种类型的REF cursor在实际的应用系统中用的也是比较多的。

create table gsm_resource      (        gsmno varchar2(11),        status varchar2(1),        price  number(8,2),        store_id varchar2(32)      );      insert into gsm_resource values('13905310001','0',200.00,'SD.JN.01');        insert into gsm_resource values('13905312002','0',800.00,'SD.JN.02');      insert into gsm_resource values('13905315005','1',500.00,'SD.JN.01');      insert into gsm_resource values('13905316006','0',900.00,'SD.JN.03');      commit;            SQL> declare        2     type gsm_rec is record(        3          gsmno  varchar2(11),        4          status varchar2(1),        5          price  number(8,2));        6          7     type app_ref_cur_type is ref cursor return gsm_rec;        8     my_cur app_ref_cur_type;        9     my_rec gsm_rec;       10         11  begin       12     open my_cur for select gsmno,status,price       13          from gsm_resource       14          where store_id='SD.JN.01';       15     fetch my_cur into my_rec;       16     while my_cur%found loop       17           dbms_output.put_line(my_rec.gsmno||'#'||my_rec.status||'#'||my_rec.price);       18           fetch my_cur into my_rec;       19     end loop;       20     close my_cur;       21  end;       22  /            13905310001#0#200      13905315005#1#500            PL/SQL procedure successfully completed    

--------------------------------------------------------------------------------------------------------------------------------------------------

/*声明部分,以declare开头*/ 
declare 
v_id integer; 
v_name varchar(20); 
cursor c_emp is select * from employee where emp_id=3; 
/*执行部分,以begin开头*/ 
begin 
 open c_emp;             --打开游标 
 loop 
  fetch c_emp into v_id,v_name;  --从游标取数据 
  exit when c_emp%notfound ; 
 end loop ; 
close c_emp;           --关闭游标 
dbms_output.PUT_LINE(v_name); 
/*异常处理部分,以exception开始*/ 
exception 
 when no_data_found then 
  dbms_output.PUT_LINE('没有数据'); 
end ;
0 0
原创粉丝点击