我需要将两个表连接在一起。加入后,我想比较50个不同的列,并列出列不匹配的每一行。有没有比解决这个问题更简单的方法
SELECT *
FROM TABLE1 T1
INNER JOIN TABLE2 T2 ON
TABLE1.ID = TABLE2.ID
WHERE T1.NAME <> T2.NAME
OR T1.DESCRIPTION <> T2.DESCRIPTON --48 more comparisons
答案 0 :(得分:0)
我认为没有一种优雅的方法可以执行此操作,但是您可以将表模式相互比较,并在列名称上进行内部联接,以生成两个表共享的列的列表。像这样:
data
然后编写一个动态查询,该查询使用该列名称列表进行比较。
答案 1 :(得分:0)
您可以使用UNION。
--sample tables
WITH tb1 as
(select 111 as id, 1 as a, 2 as b union all select 112 as id, 3 as a, 4 as b),
tb2 as
(select 111 as id, 1 as a, 2 as b union all select 112 as id, 3 as a, 5 as b),
--
tablesunion as
(select * from tb1 union
select * from tb2),
tableseq as
(SELECT ROW_NUMBER() OVER(PARTITION BY ID ORDER BY ID) AS SEQ,
*
FROM tablesunion)
SELECT tb1.*, tb2.*
FROM tableseq s
inner join tb1
on (s.id = tb1.id)
inner join tb2
on (s.id = tb2.id)
WHERE seq > 1;
结果将是:
id a b id a b
112 3 4 112 3 5