Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tsql 使用多列时不存在_Tsql_Sql Server 2005 - Fatal编程技术网

Tsql 使用多列时不存在

Tsql 使用多列时不存在,tsql,sql-server-2005,Tsql,Sql Server 2005,我正在尝试查找我的#tentable中不在staging表中的所有记录。 需要注意的是,比较需要在16个字段上进行 我试过几种组合,但似乎都不起作用 SELECT CustomerAccountNo FROM #TempTable WHERE NOT EXISTS (SELECT e.[CustomerAccountNo] , e.[MeterNo] , e.[CustomerName1] , e.[ServiceAddress1]

我正在尝试查找我的#tentable中不在staging表中的所有记录。 需要注意的是,比较需要在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          
    )

只需进行连接并查找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

尝试使用


你应该提供更详细的情况以得到正确的答案

显然,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          
    )

它怎么不起作用?你的唱片太多了吗?还不够?或者…?在这个代码组合中,我得到了所有的记录。好像什么都不见了。在其他人身上,我什么也得不到…@HABO-他们在那里,我从#诱人的e JOIN dbo看到他们
。Staging s
它需要是一个
外部
连接。否则ON子句怎么能工作?@AaronBertrand-是的,我省略了
left
。(如果你必须留下一些东西,我想是吧。)where子句只考虑16个字段中的1个。我需要确保所有16个字段都“不在”
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          
    )