2015-05-05 43 views
-1

我們有兩個銷售不同類型項目的系統。與Sandbox2中的客戶相比,我們希望在Sandbox1中顯示只有客戶的客戶。我使用了WHERE NOT EXISTS,但我不確定它是否正常工作。當我嘗試運行它時出現錯誤:將nvarchar值「MSTO15」轉換爲數據類型int時轉換失敗。所以我不確定是什麼原因造成的,或者我是否在正確的軌道上查詢。SQL查詢不存在

任何幫助,將不勝感激。

SELECT [Account #], Accounts, [Prior Annual], [Prior Annual1], [Prior Annual2] 
FROM Sandbox1.dbo.SalesHistory as s 
WHERE NOT EXISTS 
    (SELECT * 
    FROM Sandbox2.dbo.customer_master as m 
    WHERE s.[Account #] = 
      m.customer_number) 
+0

你能分享一些示例數據和要達到的效果嗎? – Mureinik

+1

s。[Account#]和m.customer_number的數據類型是什麼?根據錯誤消息,一個似乎是INTEGER,另一個是CHAR ...... – dnoeth

+0

一個是int,另一個是varchar –

回答

1
SELECT [Account #], Accounts, [Prior Annual], [Prior Annual1], [Prior Annual2] 
FROM Sandbox1.dbo.SalesHistory as s 
WHERE NOT EXISTS 
    (SELECT * 
    FROM Sandbox2.dbo.customer_master as m 
    WHERE convert(nvarchar,s.[Account #]) = 
      convert(nvarchar,m.customer_number)) 
0

試試這個:

SELECT [Account #], Accounts, [Prior Annual], [Prior Annual1], [Prior Annual2] 
FROM Sandbox1.dbo.SalesHistory as s 
where s.[Account #] not in (select [Account #] from Sandbox2.customer_master)