查找一个表中存在的列,而不是另一个表中的列.请阅读说明



我有两个版本的数据库V5和V9。 V9 是最新版本。 我必须在类似的表名中找到存在于 V9 表中而不是 V5 中的列。 例如,表名:V9 中的 ashish_table 有 5 列 A、B、C、D 和 E。 V5 中的同ashish_table表有 3 列 A、B 和 C。

因此,在 V9 的表 ashish_table 中存在两列 D 和 E,而不是在 V5 的表ashish_table中。

我已经在这两个数据库之间建立了连接。我还搜索并找到了下面的脚本,该脚本基于"data_type、data_length、data_scale、data_precision"比较表格;但它仅与具有相同名称的列匹配。 例如。它将根据data_type、data_length、data_scale、data_precision匹配 A、B 和 C 列;如果假设 A 列的数据类型已从 char 更改为 varchar,并且其他两列详细信息相同,则它将仅返回 A 列。 它没有给出 V5 ashish_table中不存在的 D 和 E。

set verify  off 
set heading off
column data_type format a20
accept table1 prompt 'Table 1: '
accept table2 prompt 'Table 2: '
prompt
prompt The table &table1 and &table2 have columns with the same name but differ:
prompt -------------------------------------------------------------------------
select 
column_name,data_type, data_length, data_scale, data_precision
from  
user_tab_columns
where 
table_name = upper('&table1')
minus
select 
column_name,data_type, data_length, data_scale, data_precision
from 
user_tab_columns
where 
table_name = upper('&table2');

感谢您阅读这个大问题。 是否有任何脚本可以比较两个表并给出缺少的列。

您可以使用以下内容来查找是否有任何额外的列:

select c2.table_name,c2.COLUMN_NAME
from all_tab_cols c2
where table_name='&table1'
and c2.COLUMN_NAME not in (select column_name 
from all_tab_cols 
where table_name='&table2');

这是未经测试的,但可以做到这一点:

select 
column_name,data_type, data_length, data_scale, data_precision
from  
user_tab_columns
where 
table_name = upper('&table1')
minus
select 
column_name,data_type, data_length, data_scale, data_precision
from 
user_tab_columns
where 
table_name = upper('&table2');
union
select c2.COLUMN_NAME, c2.data_type, c2.data_length, c2.data_scale, c2.data_precision
from all_tab_cols c2
where table_name='&table1'
and c2.COLUMN_NAME not in (select column_name 
from all_tab_cols 
where table_name='&table2');

相关内容

最新更新