我有一个数据库并且有2个模式叫DWFR和DWFR3,我需要做的是比较2 schmea并获取匹配的表名以及DWFR模式中存在的表,这在DWFR3上是不可用的以及下面的条件
SELECT schema_name, table_name
FROM tables
WHERE schema_name = 'DWFR'
AND table_name NOT LIKE 'RA%'
AND table_name NOT LIKE 'ZZ%'
AND table_name NOT LIKE 'DELETE%'
AND table_name NOT LIKE '%TEST%'
AND table_name NOT LIKE 'XX%'
AND table_name NOT LIKE 'ROB%'
AND table_name IN (
SELECT table_name
FROM tables
WHERE schema_name = 'DWFR3'
AND table_name NOT LIKE 'RA%'
AND table_name NOT LIKE 'ZZ%'
AND table_name NOT LIKE 'DELETE%'
AND table_name NOT LIKE '%TEST%'
AND table_name NOT LIKE 'XX%'
AND table_name NOT LIKE 'ROB%'
)
ORDER BY schema_name, table_name;
这将获得匹配的表名,但我也希望DWFR中的表名不是DWFR3上的表。
答案 0 :(得分:1)
您可以使用LEFT OUTER JOIN从一组中选择所有数据行,从第二组中选择匹配。
我在脚本中使用了SQL CTE公用表表达式,您可以将其视为具有其他用途的子选择
以下是您可以如何使用案例
with t2 as (
select table_name
from tables
where schema_name = 'DWFR3'
)
select t1.table_name, t2.table_name
from tables t1
left outer join t2
on t1.table_name = t2.table_name
where t1.schema_name = 'DWFR'
您还需要将附加要求或WHERE子句附加到上面的脚本
中我使用CTE的原因可以解释如下。 实际上,实际的解决方案应该如下所示。 遗憾的是,HANA似乎不支持使用JOIN上的ON子句的其他过滤条件
select t1.table_name, t2.table_name
from tables t1
left outer join tables t2
on t1.table_name = t2.table_name and t2.schema_name = 'DWFR3
where t1.schema_name = 'DWFR'
由于这个原因,我必须比应用ON,join条件更早地过滤JOIN的LEFT部分
答案 1 :(得分:0)
在两个模式中查找匹配的表名:
select
'DWFR' as schema_name,
table_name
from
(
select table_name from tables where schema_name = 'DWFR'
intersect
select table_name from tables where schema_name = 'DWFR3'
) x
where
table_name not like 'RA%' and
table_name not like 'ZZ%' and
table_name not like 'DELETE%' and
table_name not like '%TEST%' and
table_name not like 'XX%' and
table_name not like 'ROB%';
在DWFR中查找表名但不在DWFR3中查找:
select
'DWFR' as schema_name,
table_name
from
(
select table_name from tables where schema_name = 'DWFR'
except
select table_name from tables where schema_name = 'DWFR3'
) x
where
table_name not like 'RA%' and
table_name not like 'ZZ%' and
table_name not like 'DELETE%' and
table_name not like '%TEST%' and
table_name not like 'XX%' and
table_name not like 'ROB%';