查找 SQL 服务器中两个表之间的差异
Find difference between two tables in SQL Server
我有这个查询 returns 来自源 table 的 27074 行:
SELECT
[DOC NO]
,[LineNumber]
,[SHEET]
,[TestPkgNo]
,[ITEM CODE]
FROM
[excel].[dbo].[MTO2]
还有这个(第二个 table )
SELECT
DocumentNumber, LineNumber, SheetNumber, PackageNumber, ItemCode
FROM
[SPMS2].[dbo].[Materials]
JOIN
Lines ON materials.LineId = Lines.Id
JOIN
MaterialDescriptions ON MaterialDescriptions.Id = Materials.MaterialDescriptionId
JOIN
TestPackages ON TestPackages.Id = Materials.TestPackageId
这 returns 27032 行。
我想找出两个 table 之间的区别,我的意思是结果应该是这样的:
doc line sheet testpack sourcetable secondtable
** * * * ok not ok
此查询将 return 源 table 中存在但第二个 table 中不存在的所有记录。
SELECT
[DOC NO]
,[LineNumber]
,[SHEET]
,[TestPkgNo]
,[ITEM CODE]
FROM
[excel].[dbo].[MTO2] sourcetable
LEFT JOIN
(
SELECT
DocumentNumber, LineNumber, SheetNumber, PackageNumber, ItemCode
FROM
[SPMS2].[dbo].[Materials]
JOIN
Lines ON materials.LineId = Lines.Id
JOIN
MaterialDescriptions ON MaterialDescriptions.Id = Materials.MaterialDescriptionId
JOIN
TestPackages ON TestPackages.Id = Materials.TestPackageId
) secondtable
ON sourcetable.[DOC NO] = secondtable.DocumentNumber -- Remove conditions that are not needed
AND sourcetable.[LineNumber] = secondtable.LineNumber -- Remove conditions that are not needed
AND sourcetable.[SHEET] = secondtable.SheetNumber -- Remove conditions that are not needed
AND sourcetable.[TestPkgNo] = secondtable.PackageNumber -- Remove conditions that are not needed
AND sourcetable.[ITEM CODE] = secondtable.ItemCode -- Remove conditions that are not needed
WHERE secondtable.DocumentNumber IS NULL -- Assuming DocumentNumber is a non-nullable column
我有这个查询 returns 来自源 table 的 27074 行:
SELECT
[DOC NO]
,[LineNumber]
,[SHEET]
,[TestPkgNo]
,[ITEM CODE]
FROM
[excel].[dbo].[MTO2]
还有这个(第二个 table )
SELECT
DocumentNumber, LineNumber, SheetNumber, PackageNumber, ItemCode
FROM
[SPMS2].[dbo].[Materials]
JOIN
Lines ON materials.LineId = Lines.Id
JOIN
MaterialDescriptions ON MaterialDescriptions.Id = Materials.MaterialDescriptionId
JOIN
TestPackages ON TestPackages.Id = Materials.TestPackageId
这 returns 27032 行。
我想找出两个 table 之间的区别,我的意思是结果应该是这样的:
doc line sheet testpack sourcetable secondtable
** * * * ok not ok
此查询将 return 源 table 中存在但第二个 table 中不存在的所有记录。
SELECT
[DOC NO]
,[LineNumber]
,[SHEET]
,[TestPkgNo]
,[ITEM CODE]
FROM
[excel].[dbo].[MTO2] sourcetable
LEFT JOIN
(
SELECT
DocumentNumber, LineNumber, SheetNumber, PackageNumber, ItemCode
FROM
[SPMS2].[dbo].[Materials]
JOIN
Lines ON materials.LineId = Lines.Id
JOIN
MaterialDescriptions ON MaterialDescriptions.Id = Materials.MaterialDescriptionId
JOIN
TestPackages ON TestPackages.Id = Materials.TestPackageId
) secondtable
ON sourcetable.[DOC NO] = secondtable.DocumentNumber -- Remove conditions that are not needed
AND sourcetable.[LineNumber] = secondtable.LineNumber -- Remove conditions that are not needed
AND sourcetable.[SHEET] = secondtable.SheetNumber -- Remove conditions that are not needed
AND sourcetable.[TestPkgNo] = secondtable.PackageNumber -- Remove conditions that are not needed
AND sourcetable.[ITEM CODE] = secondtable.ItemCode -- Remove conditions that are not needed
WHERE secondtable.DocumentNumber IS NULL -- Assuming DocumentNumber is a non-nullable column