我有两张表要比较。除了命令,我这样做。如何知道生成的输出是更新,插入还是删除
答案 0 :(得分:0)
尝试使用FULL OUTER JOIN而不是EXCEPT比较两个表并查找插入或更新的记录。
在下面的示例中,我假设两个表都具有相同的列结构并包含主键。
SELECT CASE
WHEN T1.PK IS NULL THEN 'Records only in 2nd table'
WHEN T2.PK IS NULL THEN 'Records only in 1st table'
WHEN T1.PK=T2.PK AND (ISNULL(T1.Column1,'')<>ISNULL(T2.Column1,'') OR ISNULL(T1.Column2,'')<>ISNULL(T2.Column2,'') OR ...) THEN 'Records updated'
ELSE 'Records in both table are indentical'
END,
T1.*,
T2.*
FROM Table1 AS T1
FULl OUTER JOIN Table2 AS T2 ON T1.PK=T2.PK;