我对SQL有初学者知识,我想知道这是否可以在SQL中使用。
SQL查询1>>
select distinct(id) as active_pod from schema_naming
查询1输出>>
active_pod
DB_1
DB_2
...
DB_20
SQL查询2>>
select * from DB_1.mapping UNION
select * from DB_2.mapping UNION
....
select * from DB_20.mapping UNION
由于我对SQL的了解有限,我现在首先运行#1查询,每次更改查询2中的DB_1,DB2,... DB_20并运行#2。
但是,我想知道在一个查询中是否有这种方法,所以我不必手动更改#2查询中的DB编号,也不必每行合并。
像这样......(但不知道如何处理工会)select * from {
select distinct id from schema_naming}.user_map
如果有人能够阐明这一点,那就太好了。 (我试图在Oracle SQL上执行此操作)
提前谢谢。
答案 0 :(得分:2)
你想要得到这样的东西吗?
SELECT 'SELECT * FROM ' || active_pod || '.' || 'Mapping UNION'
FROM
(
select distinct(id) as active_pod from schema_naming
) as DT;
答案 1 :(得分:0)
这绝对是一个黑客,但它可能会让你的生活更轻松,直到提出更好的解决方案。基本上使用查询来生成第二个查询,只需要手动编辑就可以删除最后一行中不必要的UNION
。
SELECT 'SELECT * FROM ' || ACTIVE_POD || '.MAPPING UNION' AS QUERY
FROM SCHEMA_NAMING
结果:
SELECT * FROM DB_1.MAPPING UNION
SELECT * FROM DB_2.MAPPING UNION
SELECT * FROM DB_3.MAPPING UNION
SELECT * FROM DB_4.MAPPING UNION
SELECT * FROM DB_5.MAPPING UNION
SELECT * FROM DB_6.MAPPING UNION
SELECT * FROM DB_7.MAPPING UNION
SELECT * FROM DB_8.MAPPING UNION
SELECT * FROM DB_9.MAPPING UNION
SELECT * FROM DB_10.MAPPING UNION
SELECT * FROM DB_11.MAPPING UNION
SELECT * FROM DB_12.MAPPING UNION
SELECT * FROM DB_13.MAPPING UNION
SELECT * FROM DB_14.MAPPING UNION
SELECT * FROM DB_15.MAPPING UNION
SELECT * FROM DB_16.MAPPING UNION
SELECT * FROM DB_17.MAPPING UNION
SELECT * FROM DB_18.MAPPING UNION
SELECT * FROM DB_19.MAPPING UNION
SELECT * FROM DB_20.MAPPING UNION
答案 2 :(得分:0)
或者,使用PL / SQL块:
BEGIN
For i in (SELECT 'SELECT * FROM ' || ACTIVE_POD || '.MAPPING UNION' AS QUERY
FROM SCHEMA_NAMING) loop
dbms_output.put_line(i.query);
end loop;
END
您的查询将显示在IDE的输出窗口中。