select 来自 table 如果存在 否则 select 来自 oracle 中的另一个

select from a table if exist otherwise select from another in oracle

我有多个表,其中 select 列来自 示例:

select a.name, b.accno, c.bal
from tableA a, tableB b, tableC c
where a.id = d.id and a.id = c.id and a.accno ='12'

问题是 tableC 在我的架构中可能不可用。 因此,我想检查 tableC 是否可用,否则它应该在 tableA 中使用 a.bal。

请问如何构建我的 select 查询来实现这一点。 此致,

我建议您的一种解决方法是:

SELECT a.name, 
       b.accno, 
       fn_Bal(a.id, a.bal) as bal
  FROM tableA a 
  JOIN tableB b ON a.id = b.id 
 WHERE a.accno ='12';

使用函数处理 "Table or view does not exist" 异常:

create or replace function fn_bal(a_id in tableA.Id%TYPE,
                                  a_bal in tableA.bal%TYPE)
       return tableA.Bal%TYPE as

   table_does_not_exist exception;
   PRAGMA EXCEPTION_INIT(table_does_not_exist, -942);

   c_bal tableA.Bal%TYPE;

begin

  execute immediate
     'select bal
        from tableC
       where id = :1'
     into c_bal    
    using a_id;

   return c_bal;

exception
  when table_does_not_exist then
     return a_bal;
end;