怎样判断CURSOR为空?

liudj 2010-09-20 06:17:56
检索一组数据,定义一个Cursor,
Cursor打开后,如何判断Cursor是否为空?
...全文
1118 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
minitoy 2010-09-26
  • 打赏
  • 举报
回复
奇怪,代码怎么怪怪的,没语法着色了.
SQL> set serveroutput on
SQL>
SQL> declare
2 cv_c sys_refcursor;
3 zero_row_in_cursor EXCEPTION;
4
5 begin
6 open cv_c for select 1 from dual where 1=0;
7 dbms_output.put_line(cv_c%rowcount);
8 if cv_c%rowcount=0 then
9 close cv_c;
10 RAISE zero_row_in_cursor;
11 end if;
12 exception
13 when zero_row_in_cursor then
14 rollback;
15 dbms_output.put_line('error,zero row find in cursor');
16 end;
17 /

0
error,zero row find in cursor

PL/SQL procedure successfully completed

SQL>
minitoy 2010-09-26
  • 打赏
  • 举报
回复
SQL> set serveroutput on
SQL>
SQL> declare
2 cv_c sys_refcursor;
3 zero_row_in_cursor EXCEPTION;
4
5 begin
6 open cv_c for select 1 from dual where 1=0;
7 dbms_output.put_line(cv_c%rowcount);
8 if cv_c%rowcount=0 then
9 close cv_c;
10 RAISE zero_row_in_cursor;
11 end if;
12 exception
13 when zero_row_in_cursor then
14 rollback;
15 dbms_output.put_line('error,zero row find in cursor');
16 end;
17 /

0
error,zero row find in cursor

PL/SQL procedure successfully completed

SQL>
liudj 2010-09-26
  • 打赏
  • 举报
回复
谢谢各位,可能是我没有描述清楚。

我的意思是,定义一个CURSOR,处理时,如果Cursor内的ROWCOUNT为0件,抛出Warning信息,转向异常。
RowCount非空时,正常处理后面业务。
minitoy 2010-09-25
  • 打赏
  • 举报
回复
SQL> set serveroutput on
SQL>
SQL> declare
2 cv_c sys_refcursor;
3 begin
4 open cv_c for select 1 from dual where 1=0;
5 dbms_output.put_line(cv_c%rowcount);
6 close cv_c;
7 end;
8 /

0

PL/SQL procedure successfully completed

SQL>
心中的彩虹 2010-09-21
  • 打赏
  • 举报
回复
[Quote=引用楼主 liudj 的回复:]
检索一组数据,定义一个Cursor,
Cursor打开后,如何判断Cursor是否为空?
[/Quote]

--一个游标操作的模版


declare
v_name varchar2(10);
cursor cur is select name from tb;
begin
open cur;
fetch cur into v_name ;
while cursor%found loop
dbms_output.put_line(v_name);
fetch cur into v_name;
end loop;
close cur;
end;



siriusraider 2010-09-21
  • 打赏
  • 举报
回复
cursor%notfound
kpkero 2010-09-21
  • 打赏
  • 举报
回复
notfound
ngx20080110 2010-09-21
  • 打赏
  • 举报
回复

DECLARE
CURSOR your_cursor IS SELECT first_name || ' ' || last_name FROM employees WHERE salary > 10000;
fn VARCHAR2(30);
BEGIN
OPEN your_cursor;
LOOP
FETCH your_cursor INTO fn;
EXIT WHEN your_cursor%notfound;

DBMS_OUTPUT.PUT_LINE(fn);
END LOOP;
CLOSE your_cursor;
END;

-- 结果
Neena Kochhar
Lex De Haan
  • 打赏
  • 举报
回复
--给个例子
declare
cursor emp_cur is select empno,ename,sal from emp for update;
emp_row emp_cur%rowtype;
begin
open emp_cur;
loop
fetch emp_cur into emp_row;
exit when emp_cur%notfound;
if emp_row.sal<2000 then
--使用current of emp_cur时一定要for update;
update emp set sal=sal*1.1 where current of emp_cur;
end if;
end loop;
close emp_cur;
end;
sinpoal 2010-09-21
  • 打赏
  • 举报
回复

loop
fetch cursor_name into recor_type_date ;
exit when cursor_name%notfound;
end loop;
Diza1986 2010-09-21
  • 打赏
  • 举报
回复
%ROWCOUNT 整型 获得FETCH语句返回的数据行数
%FOUND 布尔型 最近的FETCH语句返回一行数据则为真,否则为假
%NOTFOUND 布尔型 与%FOUND属性返回值相反
%ISOPEN 布尔型 游标已经打开时值为真,否则为假
jonsoncheng 2010-09-20
  • 打赏
  • 举报
回复
学习了!@@@
gelyon 2010-09-20
  • 打赏
  • 举报
回复
if
cursor%notfound 
then
...
end if;
本课程采用了漫画+动手实操+练习讲授Python编程技能。课程简介:第11章 常用内置模块11.1 数学计算模块 —— math模块11.2 日期时间模块 —— datetime模块11.2.1 datetime类11.2.2 date类11.2.3 time类11.2.4 计算时间跨度类——timedelta11.2.5 将日期时间对象与字符串相互转换11.3 正则表达式模块 —— re模块11.3.1 字符串匹配11.3.2 字符串查找11.3.3 字符串替换11.3.4 字符串分割11.5 练一练第12章 文件读写12.1 打开文件12.2 关闭文件12.2.1 在finally代码块中关闭文件12.2.2 在with as代码块中关闭文件12.3 读写文本文件12.4 动动手 —— 复制文本文件12.5 读写二进制文件12.6 动动手 —— 复制二进制文件12.1 练一练第13章 图形用户界面13.1 Python中的图形用户界面开发库13.2 安装wxPython17813.3 第一个wxPython程序18013.4 自定义窗口类18213.5 在窗口中添加控件18213.6 事件处理18413.7 布局管理18513.7.1 盒子布局管理器18613.7.2 动动手——重构事件处理示例13.7.3 动动手——盒子布局管理器嵌套示例13.8 控件13.8.1 文本输入控件13.8.2 复选框和单选按钮13.8.3 列表13.8.4 静态图片控件13.9 点拨点拨 —— 如何使用wxPython官方文档13.10 练一练第14章 网络通信14.1 基本的网络知识14.1.1 TCP/IP14.1.2 IP地址14.1.3 端口14.1.4 HTTP/HTTPS14.2 搭建自己的Web服务器14.3 urllib.request模块14.3.1 发送GET请求14.3.2 发送POST请求14.4 JSON数据14.4.1 JSON文档的结构14.4.2 对JSON数据的解码14.5 动动手 —— 下载图片示例14.6 动动手 —— 返回所有备忘录信息14.7 练一练第15章 访问数据库15.1 SQLite15.1.1 SQLite的数据类型15.1.2 Python的数据类型与SQLite的数据类型的映射15.1.3 使用GUI管理工具管理SQLite15.2 数据库编程的基本操作过程15.3.1 数据库连接对象Connection15.3.2 游标对象Cursor15.4 动动手 —— 数据库的CRUD操作示例15.4.1 示例中的数据表15.4.2 无条件查询15.4.3 有条件查询15.4.4 插入数据15.4.5 更新数据15.4.6 删除数据15.5 点拨点拨 —— 防止SQL注入攻击15.6 练一练第16章 多线程16.1 线程相关的知识16.1.1 进程16.1.2 线程16.1.3 主线程16.2 线程模块 —— threading模块16.3 创建子线程16.3.1 自定义函数实现线程体16.3.2 自定义线程类实现线程体16.4 线程管理16.4.1 等待线程结束16.4.2 线程停止16.5 动动手 —— 下载图片示例16.6 练一练

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧