0

我试图在我的#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          
    )
4

3 回答 3

2

代替 a JOIN,尝试使用except。

SELECT CustomerAccountNo, MeterNo -- and so on
FROM #TempTable
EXCEPT
SELECT CustomerAccountNo, MeterNo -- and so on
FROM Staging
于 2013-04-25T19:20:35.820 回答
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          
    )
于 2013-04-25T19:32:14.003 回答
0

只需进行连接并查找空值。像这样

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
于 2013-04-25T19:19:38.407 回答