如何在pl sql函数中编写此查询,从表B中选择* if(a = 0)否则从表A中选择(a = 1)从表C中选择

时间:2015-03-15 10:57:16

标签: oracle

我使用oracle数据库,我想从表A或表B或表C中选择列条件,所以例如如果我有一个变量x, 如果有人知道解决方案或想法,请写下来 我想写一个查询:select * if (x=0) from A else if(x=1) from B else from C

我试过了,但编译失败了

create or replace 
FUNCTION return_result (



l1 number,l2 number 
)
return object_table1 as
 v_ret   object_table1;
begin

  Select object1 (ax.column1,ax column2) BULK COLLECT INTO v_ret from (


select resultat.column1,resultat column2 from resultat
case when (l2-l1>1) then from(
select * from A) resultat
.
.
.


) ax;
  return v_ret;
end return_result;`

1 个答案:

答案 0 :(得分:0)

假设TabA和TabB具有相同结构的简单示例,您可以通过以下方式实现所需的效果:

IF l1 = 0 THEN
  SELECT *  BULK COLLECT INTO v_ret  FROM tabA;
END IF;
IF l1 = 1 THEN
  SELECT *  BULK COLLECT INTO v_ret  FROM tabB;
END IF;

或者

WITH A AS ( SELECT * FROM tabA WHERE l1 = 0 ),
B AS ( SELECT * FROM tabA WHERE l1 = 1 )
SELECT * FROM A
UNION
SELECT * FROM B;