如果表1存在id=1,执行:select * from 表1,否则执行:select * from 表2
Oracle要怎么写呢?
表1和表2结构一样,只是内容不一样

解决方案 »

  1.   

    -- 可以写成这样,但是建议你分两步写比较好。select * from t1 where id = 1 
    union all
    select * from t2 where not exists(select * from t1 where id =1)
      

  2.   

    declare 
       v_cnt int;
    begin
    select count(1)  into v_cnt from 表1 where id=1 and rownum<2;
    if v_cnt>0 then
    select * from 表1
    else 
    select * from 表2
    end if;
    end;
    大概这样吧,随手写的,没校对具体语法。
      

  3.   

    SELECT 
           CASE 
                  WHEN rowcount > 0 THEN 
                  select * 
                  from   表1 
                  ELSE 
                  SELECT * 
                  FROM   表2 
           END 
    FROM   ( 
                  SELECT count(*) rowcount 
                  FROM   表1 
                  WHERE  id=1 ) a
      

  4.   

    select*from tab1 where exists(select*from tab1 t1 where t1.id = 1)
    union all
    select*from tab2 where not exists(select*from tab1 t2 where t2.id = 1)
      

  5.   

    select * from a1 where exists (select null from dual where a1.id=1)
    union all 
    select * from a2 where not exists(select null from a1 where a1.id=1)