Oracle SQL从变量集合创建虚拟表

Oracle SQL从变量集合创建虚拟表,sql,oracle,Sql,Oracle,我有5个不同的表,在所有的表中,bat_id是公共列。我有一个bat_id列表,我必须检查所有5个表中每个bat_id的记录是否存在,预期示例输出如下 示例输出: ------------------------------------------------------ l table1 l table2 l table3 l table4 l table5 l bat_id l 0 l 21 l 5 l 0 l 1 l 452 l 5

我有5个不同的表,在所有的表中,bat_id是公共列。我有一个bat_id列表,我必须检查所有5个表中每个bat_id的记录是否存在,预期示例输出如下

示例输出:

------------------------------------------------------
l table1 l table2 l table3 l table4 l table5 l bat_id
l    0   l   21   l   5    l     0  l     1  l  452
l    5   l   3    l   0    l     0  l     15 l  123
l    235 l   0    l   0    l     0  l     87 l  586
-------------------------------------------------------
我尝试了下面的查询,但一次只对一个bat_id有效。如何修改下面的查询,以检查单个查询中所有bat_id值的记录是否存在

    declare
Bat_id Number 
&Bat_id = 476097
begin
select  
(Select count(*)  from Document where bat_id=&Bat_id) as table1,
(Select count(*) from Eds_task where bat_id=&Bat_id) as table2,
(Select count(*) from MI_Doc_Reads where bat_id=&Bat_id) as table3,
(Select count(*) from MI_Batch_Status where bat_id=&Bat_id) as table4,
(Select count(*) from Batch where bat_id=&Bat_id) as table5
from dual;
end

提前感谢

您可以从
bat_id
值列表中选择,而不是从
DUAL
中选择:

select (select count(*) from table1 where bat_id = t.bat_id) as table1
, (select count(*) from table2 where bat_id = t.bat_id) as table2
, (select count(*) from table3 where bat_id = t.bat_id) as table3
, (select count(*) from table4 where bat_id = t.bat_id) as table4
, (select count(*) from table5 where bat_id = t.bat_id) as table5
, t.bat_id
from (
  select bat_id from table1
  union select bat_id from table2
  union select bat_id from table3
  union select bat_id from table4
  union select bat_id from table5
) t;
您还可以将所有表连接在一起:

select count(table1.bat_id) as table1
, count(table2.bat_id) as table2
, count(table3.bat_id) as table3
, count(table4.bat_id) as table4
, count(table5.bat_id) as table5
, coalesce(table1.bat_id,table2.bat_id,table3.bat_id,table4.bat_id,table5.bat_id) as bat_id
from table1
full outer join table2 on table1.bat_id = table2.bat_id
full outer join table3 on table1.bat_id = table3.bat_id
full outer join table4 on table1.bat_id = table4.bat_id
full outer join table5 on table1.bat_id = table5.bat_id
group by coalesce(table1.bat_id,table2.bat_id,table3.bat_id,table4.bat_id,table5.bat_id);
您可能希望使用数据检查这些查询的解释计划,并查看哪一个更适合您的情况

给定您提供的五个值,您可以将查询重写为:

select (select count(*) from table1 where bat_id = t.bat_id) as table1
, (select count(*) from table2 where bat_id = t.bat_id) as table2
, (select count(*) from table3 where bat_id = t.bat_id) as table3
, (select count(*) from table4 where bat_id = t.bat_id) as table4
, (select count(*) from table5 where bat_id = t.bat_id) as table5
, t.bat_id
from (
  select 458 as bat_id from dual
  union all select 456 from dual
  union all select 3545 from dual
  union all select 2123 from dual
  union all select 877 from dual
) t;

你的值列表来自哪里?它是什么格式的?嗨,Alex,这是Bat_id=45845635452123877的值,我应该从所有5个表中获取计数。嗨,这是Bat_id=45845635452123877的值,我应该从所有5个表中获取计数!!非常感谢