--游标的属性:found、not found、rowcount、isopen
--隐式游标:
--1、sql游标
--2、cursor for游标:无需显示声明,打开等操作
begin for te in (select * from test1 where tage>20 and tage<30) loop dbms_output.put_line('编号:'||te.tid||',名称:'||te.tname||',年龄:'||te.tage ||'备注:'||te.remark); end loop; end;
--显示游标
declare cursor cu_test1 is select tid,tname,tage,remark from test1; tid test1.tid%type; tname test1.tname%type; tage test1.tage%type; remark test1.remark%type; begin open cu_test1; fetch cu_test1 into tid,tname,tage,remark; while cu_test1%found loop dbms_output.put_line('编号:'||tid||',名称:'||tname||',年龄:'||tage ||'备注:'||remark); fetch cu_test1 into tid,tname,tage,remark; end loop; close cu_test1; end; declare cursor cu_test2 is select tid,tname,tage from test1; tid test1.tid%type; tname test1.tname%type; tage test1.tage%type; begin open cu_test2; loop fetch cu_test2 into tid,tname,tage; exit when cu_test2%notfound; dbms_output.put_line('编号:'||tid||',名称:'||tname||',年龄:'||tage); end loop; close cu_test2; end;
--使用行变量
declare cursor cu_test3 is select * from test1; te test1%rowtype; begin open cu_test3; loop fetch cu_test3 into te; exit when cu_test3%notfound; dbms_output.put_line('编号:'||te.tid||',名称:'||te.tname||',年龄:'||te.tage); end loop; close cu_test3; end;
--为游标传递参数
declare cursor cu_test4(minAge number,maxAge number) is select * from test1 where tage>minAge and tage<maxAge; te test1%rowtype; begin open cu_test4(20,30); fetch cu_test4 into te; while cu_test4%found loop dbms_output.put_line('编号:'||te.tid||',名称:'||te.tname||',年龄:'||te.tage); fetch cu_test4 into te; end loop; close cu_test4; end;
--动态游标:强类型动态游标和弱类型动态游标
--强类型动态游标:游标记录数类型固定
begin declare type test1_type is ref cursor return test1%rowtype; t_count number; te test1%rowtype; cu_test1 test1_type; begin select count(*) into t_count from test1 where tage>30; if t_count=0 then open cu_test1 for select * from test1; else open cu_test1 for select * from test1 where tage>30; end if; fetch cu_test1 into te; while cu_test1%found loop dbms_output.put_line('编号:'||te.tid||',名称:'||te.tname||',年龄:'||te.tage); fetch cu_test1 into te; end loop; end; end;
--弱类型动态游标:游标的记录类型灵活多变,这使得利用单个游标来获取不同表中的记录数
begin declare type comm_type is ref cursor; c_count number; te test1%rowtype; ma T_manager%rowtype; cu_comm comm_type; begin select count(*) into c_count from test1 where tage>40; if c_count=0 then open cu_comm for select * from T_manager; fetch cu_comm into ma; while cu_comm%found loop dbms_output.put_line('编号:'||ma.mid||',职位:'||ma.mname); fetch cu_comm into ma; end loop; else open cu_comm for select * from test1; fetch cu_comm into te; while cu_comm%found loop dbms_output.put_line('编号:'||te.tid||',名称:'||te.tname||',年龄:'||te.tage); fetch cu_comm into te; end loop; end if; end; end;
创建见附件
相关推荐
oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结
Oracle游标的一般使用
oracle游标使用大全,详细描述了如何使用游标,创建游标等等
ORACLE 游标使用示例,给大家可以共享一下。
Oracle游标使用方法及语法大全.doc
详细的oracle游标用法,简单易懂,是学习游标的好材料
oracle 游标FOR循环
Oracle游标使用方法及语法大全
oracle游标 详解 精析 示例 真正能把游标讲透、说全、调理清晰的讲义。 游标犹如C语言的指针:灵活、实用、高效。 游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。 游标是一个通过定义...
oracle游标优化以及oracle游标的作用和游标的概念。
oracle游标使用大全,其中有很多例子,有助于大家理解,希望对初学者有帮助
详细介绍了 oracle的游标使用 及 实例
Oracle 游标! 值得下载看看!资源免费,大家分享!!
关于游标使用问题的练习附答案。游标分隐式和显示游标;动态和ref游标。。。
游标是存储过程最基本的要会的,用于访问 ORACLE 数据库的 DDL 和 TCL 语
主要描述oracle中游标的学习隐式游标和显示游标
该资源系本人培训期间的关于ORCL 游标所有实用案例,在此共享希望对大家学习有所帮助
我自己编写的oracle游标案例,希望对大家有点帮助!