oracle游标的使用及属性

上传人:ths****59 文档编号:57600449 上传时间:2018-10-23 格式:DOCX 页数:7 大小:25.53KB
返回 下载 相关 举报
oracle游标的使用及属性_第1页
第1页 / 共7页
oracle游标的使用及属性_第2页
第2页 / 共7页
oracle游标的使用及属性_第3页
第3页 / 共7页
oracle游标的使用及属性_第4页
第4页 / 共7页
oracle游标的使用及属性_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《oracle游标的使用及属性》由会员分享,可在线阅读,更多相关《oracle游标的使用及属性(7页珍藏版)》请在金锄头文库上搜索。

1、游标的使用及属性游标的使用及属性 oracle 游标的使用游标的使用 游标是从数据表中提取出来的数据,以临时表的形式存放到内存中,在游标中 有一个数据指针,在初始状态下指向的是首记录,利用 fetch 语句可以移动该 指针,从而对游标中的数据进行各种操作, 然后将操作结果写回到数据库中。 一:定义游标 游标名 语句; 示例: ; ; :; ; ; 二:打开游标 语法结构: 游标名 打开游标分为两步: 将符合条件的记录送入内存 将指 针指向第一条记录 三:提取游标数据 语法形式: 游标名 变量名,变量名 ,; 或者 游标名 记录型变量名; 示例: ; ; ; :; ; ; ( ); ; 四:关闭

2、游标 游标名; Oracle 游标的属性之一游标的属性之一-%isopen %isopen 属性-测试游标是否打开,没打开的情况下使用 fetch 语句将提示 错误。 示例: set serveroutput on declare tempsal scott.emp.sal%type; cursor mycursor is select * from scott.emp where sal tempsal ; cursorrecord mycursor%rowtype; begin tempsal := 800; if mycursor%isopen then dbms_output.putl

3、ine(to_char(cursorrecord.deptno); else dbms_output.put_line(游标没有打开); end if; end; Oracle 游标的属性之二游标的属性之二-%found 该属性是测试前一个 fetch 语句是否有值,有值将返回 true ,不然 false. 示例: set serveroutpu on declare tempsal scott.emp.sal%type; cursor mycursor is select * from scott.emp where sal tempsal; cursorrecord mycursor%r

4、owtype; begin tempsal := 800; open mycursor; fetch mycursor into cursorrecord; if mycursor%found then dbms_output.put_line(to_char(cursorrecord.deptno); else dbms_outpu.put_line(没有数据); end if; end; Oracle 游标的属性之三游标的属性之三-%notfound 该属性是%found 属性的反逻辑,常被用于退出循环。 set serveroutput on declare tempsal scott.

5、emp.sal%type; cursor mycursor is select * from scott.emp where sal tempsal; cursorrecord mycursor%rowtype; begin tempsal :=800; open mycursor; fetch mycursor into cursorrecord; if mycursor%notfound then dbms_output.put_line(to_char(cursorrecord.deptno); else dbms_output.put_line(发现数据); end if; end;

6、%notfound 的理解 文档中的解释:It returns TRUE if an INSERT, UPDATE, or DELETE statement affected no rows, or a SELECT INTO statement returned no rows. Otherwise, it returns FALSE. 这个解释更加精妙: %NOTFOUND is the logical opposite of %FOUND. %NOTFOUND yields FALSE if the last fetch returned a row, or TRUE if the la

7、st fetch failed to return a row 错误的例子: tableA id name 1 a 2 b declare cursor v_cur is select name from tableA; n varchar2(10); begin open v_cur; loop exit when v_cur%notfound; fetch v_cur into n; dbms_output.put_line(n); close v_cur; end loop; end; 执行上面的语句,结果为: a b b 发现最后一条记录被打印了两次。原因是%notfound 是判断最

8、后一次 fetch 的结果,把 bfetch 到变量 n 中之后再执行 exit when %notfound 判断得到的是 false 的记过,也就是说 是有返回行的,所以判断通过,再此执行了打印语句。 发现了另一个疑问: 把 a,b 都 fetch 之后按理说游标已经空了,那么第三次应该是 fetch 的空值,为什么打印 出来的还是 b 呢? 因为 fetchinto 语句末尾不会修改 into 变量后面的值。就像 selectinto 如果没有数据会报 异常,但是不会把 into 后面的变量置为空 再写一段代码 declare cursor v_cur is select name fr

9、om tableA where name = c; n varchar2(10); begin open v_cur; loop exit when v_cur%notfound; n:=hehe fetch v_cur into n; dbms_output.put_line(n); close v_cur; end loop; end; 执行代码的结果: hehe 疑问:游标是空游标,也就是说游标在打开的时候就没有指向任何的值。但为 什么 exit when v_cur%notfound;这条语句还通过了呢? oracle 文档的解释: Before the first fetch, %N

10、OTFOUND returns NULL. If FETCH never executes successfully, the loop is never exited, because the EXIT WHENstatement executes only if its WHEN condition is true. To be safe, you might want to use the following EXIT statement instead: EXIT WHEN c1%NOTFOUND OR c1%NOTFOUND IS NULL; 也就是说 v_cur%notfound

11、有三种状态,true,false,null。所以以后为了安 全期间可以加上是否为空的判断 Oracle 游标的属性之四游标的属性之四-%rowcount 该属性用于返回游标的数据行数。 set serveroutput on declare tempsal scott.emp.sal%type; cursor mycursor is select * from scott.emp where sal tempsal; cursorrecord mycursor%rowtype; begin tempsal :=800; open mycursor; fetch mycursor into cursorrecord; dbms_output.put_line(to_char(mycursor%rowcount); end; 若返回值为 0 表明游标已经打开,但没有提取出数据。

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号