-- 可以写成这样,但是建议你分两步写比较好。select * from t1 where id = 1 union all select * from t2 where not exists(select * from t1 where id =1)
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; 大概这样吧,随手写的,没校对具体语法。
SELECT CASE WHEN rowcount > 0 THEN select * from 表1 ELSE SELECT * FROM 表2 END FROM ( SELECT count(*) rowcount FROM 表1 WHERE id=1 ) a
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)
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)
union all
select * from t2 where not exists(select * from t1 where id =1)
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;
大概这样吧,随手写的,没校对具体语法。
CASE
WHEN rowcount > 0 THEN
select *
from 表1
ELSE
SELECT *
FROM 表2
END
FROM (
SELECT count(*) rowcount
FROM 表1
WHERE id=1 ) a
union all
select*from tab2 where not exists(select*from tab1 t2 where t2.id = 1)
union all
select * from a2 where not exists(select null from a1 where a1.id=1)