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
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 ;
- PL/SQL中cursor(光标/游标)的用法
- PL/SQL中cursor(光标/游标)的用法
- pl/sql中光标cursor的使用笔记
- PL/SQL Cursor游标的基本使用
- PL/SQL 游标(cursor)的使用
- Oracle PL/SQL cursor(游标)
- PL/SQL中的游标(cursor)
- ORACLE PL/SQL游标(cursor)学习笔记
- 【Oracle】PL/SQL——游标CURSOR
- PL/SQL中Ref Cursor的应用
- Cursor游标(光标)的使用
- SQL 游标cursor的运用
- PL/SQL中游标和游标变量的使用
- PL/SQL中游标和游标变量的使用
- PL/SQL中游标和游标变量的使用
- PL/SQL中游标和游标变量的使用
- SQL Cursor(游标) 基本用法
- 在sql server中关于游标cursor的使用
- swift学习笔记(一)
- Kth Smallest Element in a BST
- java web服务器cpu占用过高的处理
- Qt问题综合
- unity5 动画系统Mecanim事件快速入门 区分animation和animator不再混淆
- PL/SQL中cursor(光标/游标)的用法
- 跟着Vamei大神-02了解play
- 疯狂猜歌实现(2)—— 实现唱片布局以及播放动画
- 自定义View时出现:"android.view.InflateException: Binary XML file line报错问题"
- 24点计算 算法
- struts2 标签的使用之一 s:if
- python 退出程序sys.exit 的退出码
- Java基础 之隐藏、复写、遮掩、遮蔽、和重载
- linux(centos)搭建SVN服务器