oracle中如何逐列比较两个表
我在两个不同的数据库中的oracle中有两个相似的表。 例如:我的表名称是 EMPLOYEE,主键是员工 ID。具有相同列的同一个表(假设 50 列是两个数据库中的 avlbl,并且两个数据库是链接的。
我想逐列比较这两个表并找出哪些记录不匹配。我想要每行中的特定列两个不匹配的表。
I have two similar tables in oracle in two different databases.
For example : my table name is EMPLOYEE and primary key is employee id. The same table with same columns(say 50 columns are is avlbl in two databases and two databases are linked.
I want to compare these two tables column by column and find out which records are not matching. i want the specific column in each row in two tables that are not matching.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果你想通过查询解决这个问题,应该可以解决这个问题
should do the trick if you want to solve this with a query
作为一种替代方案,它可以避免对每个表进行两次完整扫描,并且还为您提供了一种简单的方法来判断哪个表比另一个表具有更多具有值组合的行:
信用在这里: http://asktom.oracle.com/pls/apex/f?p=100: 11:0::::P11_QUESTION_ID:1417403971710
As an alternative which saves from full scanning each table twice and also gives you an easy way to tell which table had more rows with a combination of values than the other:
Credit goes here: http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:1417403971710
它不会很快,而且你需要输入很多内容(除非你从 user_tab_columns 生成 SQL),但是当我需要逐行和逐列比较两个表时,我会使用以下内容:柱子。
的所有行
(将排除常见的相同行)。
“PK”是构成主键的列。
如果当前行存在于 table1 中,“a”将包含 A。
如果表 2 中存在当前行,则“b”将包含 B。
编辑
添加了示例代码以显示注释中描述的差异。
每当其中一个值包含 NULL 时,结果就会不同。
It won't be fast, and there will be a lot for you to type (unless you generate the SQL from user_tab_columns), but here is what I use when I need to compare two tables row-by-row and column-by-column.
The query will return all rows that
(common identical rows will be excluded).
"PK" is the column(s) that make up your primary key.
"a" will contain A if the present row exists in table1.
"b" will contain B if the present row exists in table2.
Edit
Added example code to show the difference described in comment.
Whenever one of the values contains NULL, the result will be different.
尝试使用第 3 方工具,例如 SQL Data Examiner,它会比较 Oracle 数据库并显示差异。
Try to use 3rd party tool, such as SQL Data Examiner which compares Oracle databases and shows you differences.
使用
minus
运算符是有效的,但执行起来也需要更多时间,这是不可接受的。我对数据迁移有类似的要求,因此我使用了 NOT IN 运算符。
修改后的查询是:
该查询执行速度很快。您还可以在选择查询中添加任意数量的列。
唯一的问题是两个表应该具有完全相同的表结构才能执行。
Using the
minus
operator was working but also it was taking more time to execute which was not acceptable.I have a similar kind of requirement for data migration and I used the
NOT IN
operator for that.The modified query is :
This query executed fast. Also you can add any number of columns in the select query.
Only catch is that both tables should have the exact same table structure for this to be executed.
使用完整外连接 -- 但不会显示 - 如果不匹配 -
SQL> desc aaa - 它是一张桌子
名字为空?输入
A1 号码
B1 VARCHAR2(10)
SQL> desc aaav -它是一个视图
名字为空?输入
A1 号码
B1 VARCHAR2(10)
SQL> select a.column_name,b.column_name from dba_tab_columns a full external join dba_tab_columns b on a.column_name=b.column_name where a.TABLE_NAME='AAA' and B.table_name='AAAV';
COLUMN_NAME COLUMN_NAME
A1 A1
B1 B1
Used full outer join -- But it will not show - if its not matched -
SQL> desc aaa - its a table
Name Null? Type
A1 NUMBER
B1 VARCHAR2(10)
SQL> desc aaav -its a view
Name Null? Type
A1 NUMBER
B1 VARCHAR2(10)
SQL> select a.column_name,b.column_name from dba_tab_columns a full outer join dba_tab_columns b on a.column_name=b.column_name where a.TABLE_NAME='AAA' and B.table_name='AAAV';
COLUMN_NAME COLUMN_NAME
A1 A1
B1 B1