我正在使用 ORACLE 数据库。 编辑:
我只想要列表中不存在的表,而不是数据库中的所有表。
例如:在我的数据库中存在以下表格:
a
b
c
d
e
r
s
我得到了一个表格列表:
a
b
c
v
h
其中我想找出我的数据库中哪些表不存在(不存在)。
所以:a、b、c 是我的数据库中存在的那些表。因此,排除这些表格,我的答案将是:
v and h
我的答案不是 d,e,r,s
我怎样才能为此编写查询以找出答案?
您可以使用
SELECT a.table_name FROM (SELECT 'A' as table_name
FROM DUAL UNION
SELECT 'B' as table_name
FROM DUAL UNION
SELECT 'C' as table_name
FROM DUAL UNION
SELECT 'V' as table_name
FROM DUAL UNION
SELECT 'H' as table_name
FROM DUAL) a WHERE NOT EXISTS (
SELECT 1 FROM DBA_TABLES b where b.table_name=a.table_name)
WITH table_names AS (
SELECT 'A' AS tname FROM DUAL
UNION ALL
SELECT 'B' FROM DUAL
UNION ALL
SELECT 'C' FROM DUAL
UNION ALL
SELECT 'D' FROM DUAL
UNION ALL
SELECT 'E' FROM DUAL
)
SELECT tn.tname
FROM table_name tn
WHERE NOT EXISTS (SELECT 1
FROM all_tables at
WHERE tn.tname = at.table_name);
WITH 部分只是编写派生表(“内联视图”)的一种不同(可重用)方式。
我会用 MINUS 运算符(总结如下):
select table_name from table_list
minus
select table_name from db_tables
;
更全面:
with table_list as
( select 'A' table_name from dual
union select 'B' table_name from dual
union select 'C' table_name from dual
union select 'V' table_name from dual
union select 'H' table_name from dual
)
, db_tables as
( select 'A' table_name from dual
union select 'B' table_name from dual
union select 'C' table_name from dual
union select 'D' table_name from dual
union select 'E' table_name from dual
union select 'R' table_name from dual
union select 'S' table_name from dual
)
select table_name from table_list
minus
select table_name from db_tables
;
SELECT table_name FROM DBA_tables WHERE table_name IN (Yourlist comes here)
例如
SELECT table_name FROM DBA_tables WHERE table_name NOT IN ('a','b','c');
您可以使用该表和 sys.dbms_debug_vc2coll 从您的列表中创建一个虚拟列。
以下应该可以解决问题:
SELECT
column_value AS missing_table_names
FROM
TABLE(SYS.DBMS_DEBUG_VC2COLL('a','b','c','v','h'))
WHERE
column_value NOT IN (
SELECT
table_name
FROM
ALL_TABLES
);