各位朋友:
  有个select语句像这样: 
  select book.name from book where XXX;
where条件能保证这个select结果一定是一个值,而不是一组值。当select结果是空的,提示没找到数据(可能book表是空表),这种情况下我想对name按默认值来操作;否则,当select结果不空时,就按结果值来操作。我该怎么写呢??求指教。

解决方案 »

  1.   

    我的代码是这样的,这是个自定义函数:
    --前面略去了声明之类的
    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;
      

  2.   

    nvl2()方法不可以吗?
    select nvl2(name,name,'没有记录') from zzgtest?
    是我想得太简单了 哈?
      

  3.   


    额  谢谢你的回答!但是zzgtest表是空表的话,会提示“未找到数据”,而不是继续按默认值来执行,这应该是个异常,这时该怎么办呢?我就是想问这种情况下的处理方法。
      

  4.   

      select orders.bsum into book_sum from classes, orders
       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;