各位朋友:
有个select语句像这样:
select book.name from book where XXX;
where条件能保证这个select结果一定是一个值,而不是一组值。当select结果是空的,提示没找到数据(可能book表是空表),这种情况下我想对name按默认值来操作;否则,当select结果不空时,就按结果值来操作。我该怎么写呢??求指教。
有个select语句像这样:
select book.name from book where XXX;
where条件能保证这个select结果一定是一个值,而不是一组值。当select结果是空的,提示没找到数据(可能book表是空表),这种情况下我想对name按默认值来操作;否则,当select结果不空时,就按结果值来操作。我该怎么写呢??求指教。
解决方案 »
- Oracle关于instr和like的几个问题
- Oralce 写函数的问题
- 提问,监听器和本地服务,以及用PL/SQL连接数据库的问题
- 关于怎样把一张表中的数据导入到另一张表的问题?
- 急难存储过程,请版主beckhambobo和各位高手来指点!小弟先谢了!!!
- 用DELPHI连接ORACLE的表,表里有BLOB字段,打开表就出现无效的字段,怎么解决
- pl/sql简单问题:
- 请问高人如何在ORACLE中根据系统时间设置触发器。
- 谁能将在ORACLE存贮过程中,使用游标的情况给讲解一下,最好提供几个例子。
- 关于select 语句的问题
- Oracle查询语句求指教,大师进。
- win7下安装oracle的问题
--前面略去了声明之类的
begin
open book_info_cur; --这是个游标
fetch book_info_cur into my_book_name, book_author, book_bno;
loop
exit when not book_info_cur%found;
--就是这个select语句,book表这时是空的,所以select结果是空的,这时我想对book_sum以默认值来操作
select orders.bsum into book_sum from classes, orders
where orders.term=selected_term and
orders.clno=current_clno and
orders.bno=book_bno;
if not sql%found then
--这里用异常好像解决不了,如果捕获到了异常的话,是不是整个程序就退出了?
raise no_data_found;
else
--当前记录的相关值付给局部变量
tmp_str := tmp_str||'/'||my_book_name||'-'||book_author||'-'||book_sum;
end if;
exception
when no_book_sum_found then book_sum:=0;
fetch book_info_cur into my_book_name, book_author, book_bno;
end loop;
close book_info_cur;
return (tmp_str);
end get_books;
select nvl2(name,name,'没有记录') from zzgtest?
是我想得太简单了 哈?
额 谢谢你的回答!但是zzgtest表是空表的话,会提示“未找到数据”,而不是继续按默认值来执行,这应该是个异常,这时该怎么办呢?我就是想问这种情况下的处理方法。
where orders.term=selected_term and
orders.clno=current_clno and
orders.bno=book_bno;
改成
begin
select orders.bsum into book_sum from classes, orders
where orders.term=selected_term and
orders.clno=current_clno and
orders.bno=book_bno;
EXCEPTION WHEN NO_DATA_FOUND THEN
--没有值时你想干的事情
end;