declare
   v_name     varchar2(10) := null;
begin
   begin
      select 用户姓名 into v_name from 表A where 用户id ='0000011' and rownum = 1 ;
   exception when no_data_found then 
      begin
         select 用户姓名 into v_name from 表B where 用户id='0000011'and rownum = 1;
      exception when no_data_found then null;
      end;
   end;
end;

解决方案 »

  1.   

    select 用户姓名 into v_name from 表A where 用户id='0000011';
    if v_name is null then
      select 用户姓名 into v_name from 表B where 用户id='0000011'; 
    end if;
      

  2.   

    begin
      begin
      select 用户姓名 into v_name from 表A where 用户id='0000011';
      exception
      when others then
      begin
      select 用户姓名 into v_name from 表B where 用户id='0000011';    
      exception
      when others then
      ....
      end;
      end;
    end;
      

  3.   

    加到100分!!!
    oracle 7.3 中,不用eception 有没有更好的方法实现下面的功能。
    select count(*) into i from 表A where 用户id='0000011';
      if i>0 then 
        select 用户姓名 into v_name from 表A where 用户id='0000011';
      else 
        select count(*) into  j from 表B where 用户id='0000011';
        if j>0 then 
         select 用户姓名 into v_name from 表B where 用户id='0000011';    end if;
     end if 
      

  4.   

    select nvl(max(用户姓名),'null') into v_name from 表A where 用户id='0000011';
    if v_name='null' then
      select nvl(max(用户姓名),'null') into v_name from 表B where 用户id='0000011';
      if v_name='null' then
      ...
      end if;
    end if;
    ....
      

  5.   

    为什么要加max() ,用户id='0000011' 只有一条数据呀?
      

  6.   

    SQL> select nvl(max(time),0) from test where 1=2;NVL(MAX(TIME),0)
    ----------------
                   0SQL> select nvl(time,0) from test where 1=2;未选定行SQL> 使用max(),如果没有检索出结果 返回null
    不用的话,触发exception操作
      

  7.   

    是不是我的oracle 7.3 不可以用exists如: if exists(select * from ……) then 
      ……
      end if;
      

  8.   

    select count(*) into i from 表A where 用户id='0000011';
      if i>0 then 
        select 用户姓名 into v_name from 表A where 用户id='0000011';
      else 
        select count(*) into  j from 表B where 用户id='0000011';
        if j>0 then 
         select 用户姓名 into v_name from 表B where 用户id='0000011';    end if;
     end if 
      

  9.   

    robixiao(阿喜) 写的格式很好啊
      

  10.   

    有朋友建议我用rownum,即:
    select count(*) into i from 表A where 用户id='0000011' and  rownum=1;
      if i>0 then 
        select 用户姓名 into v_name from 表A where 用户id='0000011';
      else 
        select count(*) into  j from 表B where 用户id='0000011' and  rownum=1;
        if j>0 then 
         select 用户姓名 into v_name from 表B where 用户id='0000011';    end if;
     end if ;
    原先select count(*) into i from 表A where 用户id='0000011';要扫描整个表A来count(*),这并不是程序本意。程序里只要判断表A是否存在用户id='0000011'的记录就行。
    加上 rownum=1后可以起到相同的功能,而且缩短了查询时间,这方法不错!
       各位觉得如何?
      
      

  11.   

    加上 rownum=1后可以起到相同的功能,而且缩短了查询时间,这方法不错 -->有想法,好
      

  12.   

    你的程序,分析一下:
    1.如果表中有错误:i=2
    将报出错误:select 用户姓名 into v_name from 表A where 用户id='0000011';
    2.如果 用户id没建索引.
    count(*) 肯定全表扫描.性能恶化.select NVL(用户姓名,'NULL') into v_name from 表B where 
    exists (select 1 from 表a where 表a.用户id=表B.用户id and 表a.用户id='0000011' and rownum=1)
    if v_name='NULL' then
        select 用户姓名 into v_name from 表A where 用户id='0000011' and rownum=1;end if;
      

  13.   

    “1.如果表中有错误:i=2”
    是什么意思?
    “2.如果 用户id没建索引. count(*) 肯定全表扫描.性能恶化.”
    你是说select count(*) into i from 表A where 用户id='0000011' and  rownum=1;
    也要做全表扫描吗?
      

  14.   

    1.i=2.
    select 用户姓名 into v_name from 表A where 用户id='0000011';
    就存在二个姓名了,into v_name会报措.
    2.count(*)如果你用户id没有索引会出现TABLE ACCESS FULL .你可以试一下,看一下执行计划.                           
      

  15.   

    to:
    select 用户姓名 into v_name from 表A where 用户id='0000011' and rownum=1;
    后面的rownum=1;也不能阻止TABLE ACCESS FULL 吗?
      

  16.   

    rownum=1当然可以防止TABLE ACCESS FULL!!!!^_^
      

  17.   

    rownum=1 在数据库中怎么取数据的?ID    年龄  姓名
    1     20    张三
    33    20    里斯
    345   20    往无select * from emp where 年龄=20 and rownum=1;
    到底返回哪一行?是不是一定为 张三 所在的行
    如果删除 张三 所在的行呢?
      

  18.   

    select 用户姓名,'A' into v_name ,v_flag from A
    union
    select 用户姓名,'B' into v_name ,v_flag from B
    不知道这样行不行?
      

  19.   

    TO nicolas1999king
    这样从逻辑上来说,如果A、B表中都能检索出结果,v_flag 是放'A' 还是'B'呢?
      

  20.   

    我试了一下,用exist会更快些。 
    select count(*) into i from dual where exists (select  1 from 表A where 用户id='0000011')
      if i>0 then 
        select 用户姓名 into v_name from 表A where 用户id='0000011';
      else 
        select count(*) into j  from dual where exists (select  1 from 表B where 用户id='0000011')
        if j>0 then 
         select 用户姓名 into v_name from 表B where 用户id='0000011';    end if;
     end if 谢谢 各位的关注。