Oracle 游标
2016-09-09 10:11
155 查看
1、概念
游标是指向SQL处理的内存区的句柄或指针。当使用一个PL/SQL块来执行DML语句或只返回一行结果的SELECT语句时,系统将自动创建一个隐式游标。如果SQL语句返回多个结果,就必须创建一个显示游标。
2、隐式游标例程
3、显示游标
3.1、定义游标
3.2、一些使用游标的例子
3.3、在存储过程中返回游标
游标是指向SQL处理的内存区的句柄或指针。当使用一个PL/SQL块来执行DML语句或只返回一行结果的SELECT语句时,系统将自动创建一个隐式游标。如果SQL语句返回多个结果,就必须创建一个显示游标。
--游标的属性 --(1)cur_name%rowcount :指出处理的行数 -- (2) cur_name%found :处理了一行或多行返回TRUE否则FALSE 如 WHILE CUR%FOUND中 --(3)cur_name%notfound :如果没有处理行返回TRUE,否则FALSE 如 EXIT WHEN CUR%NOTFOUND --(4)cur_name%isopen :如果处理之后不关闭游标,则为TRUE,关闭后为FALSE。发生在隐式游标中时 -- 总是为FALSE;
2、隐式游标例程
隐式游标,系统自动声明,自动打开,自动使用并且自动关闭。
declare tname student.name%type; tage student.age%type; begin select name,age into tname,tage from student where id='S001'; --如果返回多行,或零行,执行将报错。 dbms_output.put_line('姓名='||tname||' 年龄='||tage); end;
3、显示游标
3.1、定义游标
-- 定义游标(1) -- CURSOR cursor_name IS select_statement; cursor c_stu is select * from student; --------------------------------------------------------------------------------------- -- 定义系统游标(2) ,使用参照3 -- cursor_name SYS_REFCURSOR;系统游标 c_stu sys_refcursor; --------------------------------------------------------------------------------------- --定义游标(3) --先在包中定义游标,及用于检索的结构体, --这里的结构体相当于 游标%rowtype create or replace package p_stu as type c_stu is ref cursor; type rc_stu is record ( name student.name%type, age student.age%type ); end; --使用包中的游标 declare c_student p_stu.c_stu; crw_stu p_stu.rc_stu; begin open c_student for select name,age from student; loop fetch c_student into crw_stu; exit when c_student%notfound; dbms_output.put_line('姓名='||crw_stu.name||' 年龄='||crw_stu.age); end loop; end;
3.2、一些使用游标的例子
--使用 FOR 循环遍历游标,不需要明确打开和关闭游标 declare --定义一个游标 cursor c_stu is select name,age from student; --定义一个游标变量 cw_stu c_stu%rowtype; begin --使用for循环来使用这个游标 for cw_stu in c_stu loop dbms_output.put_line('姓名='||cw_stu.name||' 年龄='||cw_stu.age); end loop; end;
--使用 fetch 游标 必须明确打开和关闭游标 declare --定义一个游标 cursor c_s 4000 tu is select name,age from student; --定义一个游标变量 cw_stu c_stu%rowtype; begin --明确打开游标 open c_stu; loop --提取一行数据到cw_stu fetch c_stu into cw_stu; --判读是否提取到值,没取到值就退出 --取到值c_stu%notfound 是false --取不到值c_stu%notfound 是true exit when c_stu%notfound; dbms_output.put_line('姓名='||cw_stu.name||' 年龄='||cw_stu.age); end loop; --明确关闭游标 close c_stu; end;
--使用 while fetch 遍历数据 %found属性 declare --定义一个游标 cursor c_stu is select name,age from student; --定义一个游标变量 cw_stu c_stu%rowtype; begin open c_stu; --明确打开游标 fetch c_stu into cw_stu; --填充第一行数据,以便WHILE条件检索 while c_stu%found loop dbms_output.put_line('姓名='||cw_stu.name||' 年龄='||cw_stu.age); fetch c_stu into cw_stu; --每次检索前都需要填充数据 end loop; close c_stu; --明确关闭游标 end;
--带参游标 declare cursor c(sSal emp.sal%type, sEmpno emp.empno%type) is select * from emp where sal >= sSal and empno > sEmpno; begin for record_data in c(2500, 6666) loop dbms_output.put_line(record_data.ename); end loop; end;
3.3、在存储过程中返回游标
-- 注意:在declare块中使用存储过程返回的游标时: -- (1)不能定义系统游标变量,编译错误。如:cw_Stu C_Stu%rowtype; -- (2)可以使用结构体变量,但不能使用for循环如:for rw_stu in c_stu loop -- 将提示 c_stu '不是过程或尚未定义'。 -- (3)游标不可显示打开或关闭,如 open c_stu;表达式类型错误。
相关文章推荐
- Oracle 游标(cursor) 说明
- ORACLE中游标的使用方法
- Oracle中游标Cursor基本用法详解
- Oracle REF动态游标使用
- oracle:游标使用样例
- oracle 游标练习
- Java使用Oracle遇到的最大游标超出问题及其解决方法
- 使用Oracle显式游标及for循环
- oracle游标(返回多条数据)分页
- oracle游标属性
- Oracle 游标使用全解
- Oracle中使用游标获取指定数据表的所有字段名对应的字符串
- oracle 游标
- Oracle 中使用 fetch bulk collect into 批量效率的读取游标数据【转】
- java通过prepareCall调用oracle返回游标的存储过程
- oracle使用六(游标管理)
- oracle 游标变量ref cursor详解
- Oracle中清除游标缓存的几种方法
- Oracle 游标for循环
- Oracle 游标使用全解