create table test1 (t_cloum varchar2(100));
insert into test1 values('123');CREATE OR REPLACE FUNCTION testszz(v_rybh VarChar2) Return VarChar2 As
 tc_rysx VarChar2(100) Default NULL;
Begin
  select  t_cloum into tc_rysx from test1 where t_cloum = v_rybh;
  if tc_rysx is not null then
  RETURN '123';
  else
  RETURN '456';
  end if;
End testszz;select testszz('789') from dual为什么不走else呢?

解决方案 »

  1.   

    select x into x这条语句没有值时,会抛出异常,下面代码就不会执行了,end上加一个异常处理。exception
      when no_data_found then
          return '789';然后你试试
      

  2.   

    select x into x这条语句没有值时,会抛出异常,下面代码就不会执行了,end上加一个异常处理。

exception
  when no_data_found then
      return '789';

然后你试试
      

  3.   

    return 跳出整个循环,本循环后面的不再执行改成print  或者 dbms
      

  4.   


    -- 这种形式
    declare 
        v_count int ; 
    begin
        select count(*) into v_count from user_objects where object_id = 100;
        if  v_count > 0  then
            dbms_output.put_line(100);
        else
            dbms_output.put_line(100);
        end if ;
    end ;     
      

  5.   

    CREATE OR REPLACE FUNCTION testszz(v_rybh VarChar2) Return VarChar2 As
      tc_rysx VarChar2(100) Default NULL;
    Begin
      select t_cloum into tc_rysx from test1 where t_cloum = v_rybh;
      if tc_rysx is not null then
        RETURN '123';
      end if;
    exception
      when no_data_found then
        return '456';
    End testszz;
    --在用select。。into。。给变量赋值时,如果没有返回数据,则会出现no_data_found 的预定义异常
                                                                   如果返回多条数据, 则会出现too_many_rows  的预定义异常
    在你的语句中,v_rybh除了赋值‘123’之外,其他的数值都会引发no_data_found 的预定义异常