从表中选择(如果存在),否则从oracle中的另一个选择

时间:2018-11-05 10:57:59

标签: sql oracle oracle11g oracle10g

我有多个表,我从中选择列 示例:

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。

请如何构建我的选择查询以实现此目的。 问候

1 个答案:

答案 0 :(得分:0)

我建议您采用一种解决方法:

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';

使用函数来处理“表或视图不存在”异常:

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;
相关问题