我正在尝试查找#TempTable中不在临时表中的所有记录。 重要的是要注意,比较需要在16个领域进行。
我尝试了几种组合,但似乎没有任何效果。
SELECT CustomerAccountNo FROM #TempTable
WHERE NOT EXISTS
(SELECT e.[CustomerAccountNo] ,
e.[MeterNo] ,
e.[CustomerName1] ,
e.[ServiceAddress1] ,
e.[ServiceAddress2] ,
e.[ServiceCity] ,
e.[ServiceState] ,
e.[ServiceZip] ,
e.[BillingAddress1] ,
e.[BillingAddress2] ,
e.[BillingAddress3] ,
e.[BillingCity] ,
e.[BillingState] ,
e.[BillingZip] ,
e.[BillingZip4] ,
e.[PrimaryPhoneNumber] FROM #TempTable e
JOIN dbo.Staging s
ON e.CustomerAccountNo = s.CustomerAccountNo AND
e.MeterNo = s.MeterNo AND
e.CustomerName1 = s.CustomerName1 AND
e.ServiceAddress1 = s.ServiceAddress1 AND
e.ServiceAddress2 = s.ServiceAddress2 AND
e.ServiceCity = s.ServiceCity AND
e.ServiceState = s.ServiceState AND
e.ServiceZip = s.ServiceZip AND
e.BillingAddress1 = s.BillingAddress1 AND
e.BillingAddress2 = s.BillingAddress2 AND
e.BillingAddress3 = s.BillingAddress3 AND
e.BillingCity = s.BillingCity AND
e.BillingState = s.BillingState AND
e.BillingZip = s.BillingZip AND
e.BillingZip4 = s.BillingZip4 AND
e.PrimaryPhoneNumber= s.PrimaryPhoneNumber
)
答案 0 :(得分:2)
而不是JOIN
,请尝试使用Except.
SELECT CustomerAccountNo, MeterNo -- and so on
FROM #TempTable
EXCEPT
SELECT CustomerAccountNo, MeterNo -- and so on
FROM Staging
答案 1 :(得分:0)
只需进行连接并查找null。喜欢这个
SELECT e.*
FROM #TempTable e
LEFT JOIN dbo.Staging s
ON e.CustomerAccountNo = s.CustomerAccountNo AND
e.MeterNo = s.MeterNo AND
e.CustomerName1 = s.CustomerName1 AND
e.ServiceAddress1 = s.ServiceAddress1 AND
e.ServiceAddress2 = s.ServiceAddress2 AND
e.ServiceCity = s.ServiceCity AND
e.ServiceState = s.ServiceState AND
e.ServiceZip = s.ServiceZip AND
e.BillingAddress1 = s.BillingAddress1 AND
e.BillingAddress2 = s.BillingAddress2 AND
e.BillingAddress3 = s.BillingAddress3 AND
e.BillingCity = s.BillingCity AND
e.BillingState = s.BillingState AND
e.BillingZip = s.BillingZip AND
e.BillingZip4 = s.BillingZip4 AND
e.PrimaryPhoneNumber= s.PrimaryPhoneNumb
WHERE s.CustomerAccountNo is null
答案 2 :(得分:0)
您应该提供更详细的情况以获得正确的答案。
显然,您的FROM子句和WHERE子句之间没有任何连接,因此查询将全部返回。
试试这个:
SELECT CustomerAccountNo FROM #TempTable t
WHERE NOT EXISTS
(SELECT 1 FROM dbo.Staging s WHERE
t.CustomerAccountNo = s.CustomerAccountNo AND
t.MeterNo = s.MeterNo AND
t.CustomerName1 = s.CustomerName1 AND
t.ServiceAddress1 = s.ServiceAddress1 AND
t.ServiceAddress2 = s.ServiceAddress2 AND
t.ServiceCity = s.ServiceCity AND
t.ServiceState = s.ServiceState AND
t.ServiceZip = s.ServiceZip AND
t.BillingAddress1 = s.BillingAddress1 AND
t.BillingAddress2 = s.BillingAddress2 AND
t.BillingAddress3 = s.BillingAddress3 AND
t.BillingCity = s.BillingCity AND
t.BillingState = s.BillingState AND
t.BillingZip = s.BillingZip AND
t.BillingZip4 = s.BillingZip4 AND
t.PrimaryPhoneNumber= s.PrimaryPhoneNumber
)