如何在oracle中逐列比较两个表

Def*_*ore 9 sql database oracle database-design oracle10g

我在两个不同的数据库中有两个类似的oracle表.例如:我的表名是EMPLOYEE,主键是员工ID.具有相同列的相同表(例如,50列是两个数据库中的avlbl,并且链接了两个数据库).

我想逐列比较这两个表,找出哪些记录不匹配.我希望两个表中每行中的特定列不匹配.

mca*_*ral 15

select *
from 
(
( select * from TableInSchema1
  minus 
  select * from TableInSchema2)
union all
( select * from TableInSchema2
  minus
  select * from TableInSchema1)
)
Run Code Online (Sandbox Code Playgroud)

如果你想用查询来解决这个问题,应该这样做

  • +1您可能还想为每个查询添加一列,以指示数据的来源.例如:"从TableInSchema1中选择1个schema1Or2,TableInSchema1.*减去......".然后在一些值和新列的结束顺序,例如"按2,3,4,5,1顺序排序".然后你(可能)将相关的行彼此相邻,并且很明显有什么不同而不是缺少什么. (4认同)

Pat*_*and 6

作为一种替代方法,它可以避免对每个表进行两次完全扫描,并且还为您提供了一种简单的方法来判断哪个表的值组合比另一个表多:

SELECT col1
     , col2
     -- (include all columns that you want to compare)
     , COUNT(src1) CNT1
     , COUNT(src2) CNT2
  FROM (SELECT a.col1
             , a.col2
             -- (include all columns that you want to compare)
             , 1 src1
             , TO_NUMBER(NULL) src2
          FROM tab_a a
         UNION ALL
        SELECT b.col1
             , b.col2
             -- (include all columns that you want to compare)
             , TO_NUMBER(NULL) src1
             , 2 src2
          FROM tab_b b
       )
 GROUP BY col1
        , col2
HAVING COUNT(src1) <> COUNT(src2) -- only show the combinations that don't match
Run Code Online (Sandbox Code Playgroud)

信用在这里:http : //asktom.oracle.com/pls/apex/f?p=100 :11:0 ::::P11_QUESTION_ID : 1417403971710