iv_tag:=0;select tag
into iv_tag
from table
where ...当搜索的结果为空时,会报no_data_found异常,如何实现从表中找到数据,当没有结果就返回一个0给iv_tag而不是报异常;

解决方案 »

  1.   

    the same question ..you can search tbis question by csdn  :-)
      

  2.   

    select nvl(tag) into iv_tag from table  where ...
      

  3.   

    只nvl不行,要用select nvl(max(tag),0) 
    into iv_tag
    from table
    where ...
      

  4.   

    前几天在别的帖子里面发的,ls为什么非要用max?
    CREATE TABLE TABLE1(ID INT,condition VARCHAR2(10));
    INSERT INTO TABLE1 VALUES(1,'A');
    INSERT INTO TABLE1 VALUES(2,'B');
    INSERT INTO TABLE1(condition) VALUES('C');
    COMMIT;create or replace procedure MYPROCDURE(condition_para varchar2) as
    result_id INT;
    begin
    select NVL(id,0) as id into result_id from TABLE1 where condition = condition_para;
    dbms_output.put_line(result_id);
    end;
    /
    SQL> set serveroutput on
    SQL> EXEC MYPROCDURE('A');
    1PL/SQL 过程已成功完成。SQL> EXEC MYPROCDURE('C');
    0PL/SQL 过程已成功完成。
      

  5.   

    --设置no_data_found异常
    iv_tag:=0;select tag
    into iv_tag
    from table
    where ...exception
    when no_data_found then
    iv_tag:=0;
      

  6.   

    楼上的正解.
    不是因为你没取到值,而是因为你没有找到值
    如果在存储过程中的select语句没有找到值就会出现你那个错误
    你可以增加一个容错处理
    begin
       select ..
    exception 
      when no_data_found then 
        --处理
    end ;
      

  7.   

    用不着maxselect nvl(tag,0)
    into iv_tag
    from table
    where ...
      

  8.   

    楼上有几位似乎没理解楼主的意思. 不是取取值为空, 而是没有记录...
    aaa number:=0;begin 
        select 1 into aaa from dual where 1=2;
    exception 
        when no_data_found then 
            aaa:=-1;
        when others then 
            aaa:=2;
    end ;
      

  9.   

    不用max的,
    试试 EXEC MYPROCDURE('X');
      

  10.   

    用exception 的,在过程里没办法再向下处理了吧?
      

  11.   

    编程动力 www.bcexe.com 专业的编程开发类网站,网络编程,软件开发,网站开发,一切尽在这里!
      

  12.   

    先用select count判断结果是否用,然后用if then语句进行处理
      

  13.   

    iv_tag:=0;
    lcounts:number;select count(1) into lcounts
    from table
    where ...if lcounts<>1 then 
      --跳出去
    else
     
    end if  select tag
    into iv_tag
    from table
    where ...
      

  14.   

    iv_tag:=0;
    lcounts:number;select count(1) into lcounts
    from table
    where ...if lcounts<>1 then 
      --跳出去
    else
    select tag
    into iv_tag
    from table
    where ...
     
    end if;  
      

  15.   

    pathfinder163() 说的对
    不过办法很多  此种最简单
      

  16.   

    select nvl(colname ,用来替换null的值 )
      

  17.   

    ...
    begin
         select a into v_a from tab where ...;
    exception
         when no_data_found then 
             v_a := 0;
         when others then 
            --其他异常,由业务逻辑决定如何处理
    end ;
    ...注意NO_DATA_FOUND异常不是对应记录相应列为null,而是没有符合条件的记录。
      

  18.   

    select nvl(tag,'你指定表示空的字符串')
    into iv_tag
    from table
    where ...
      

  19.   

    用exception的话,就执行不到之后的语句了
      

  20.   

    如果是一行都取不到的话,那用nvl也没用........
    还是取数据前先做一下count比较好
      

  21.   

    iv_tag:=0;
    select nvl(A.tag,0) 
    into iv_tag
    from (select tag ,0 as temp table) A ,(select 0 as temp from dual) B
    where A.temp (+)=B.temp
    ...
    已试过,没有问题