SQL 查询不存在
SQL Query Not Exists
我们有两个销售不同类型物品的系统。与 Sandbox2 中的帐户相比,我们希望显示在 Sandbox1 中只有帐户的客户。我使用了 WHERE NOT EXISTS,但我不确定它是否正常工作。当我尝试 运行 时出现错误:将 nvarchar 值 'MSTO15' 转换为数据类型 int 时转换失败。所以我不确定是什么原因造成的,或者我的查询是否在正确的轨道上。
如有任何帮助,我们将不胜感激。
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)
试试这个:
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)
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))
我们有两个销售不同类型物品的系统。与 Sandbox2 中的帐户相比,我们希望显示在 Sandbox1 中只有帐户的客户。我使用了 WHERE NOT EXISTS,但我不确定它是否正常工作。当我尝试 运行 时出现错误:将 nvarchar 值 'MSTO15' 转换为数据类型 int 时转换失败。所以我不确定是什么原因造成的,或者我的查询是否在正确的轨道上。
如有任何帮助,我们将不胜感激。
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)
试试这个:
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)
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))