简体   繁体   English

SQL Server:多个WHERE NOT EXISTS子句

[英]SQL Server : multiple WHERE NOT EXISTS clauses

I have about 2600 rows in the Load_Charges_IMPORT query that are not being inserted into the Load_Charges query. 我在Load_Charges_IMPORT查询中有大约2600行没有插入到Load_Charges查询中。

I am trying to insure that no duplicate primary key entries are added. 我试图确保没有重复的主键项被添加。 The primary key is established in the Load_Charges query as compound key (Charge Description + Charged Amount). 主键在Load_Charges查询中建立为复合键(收费说明+收费金额)。 No keys are set in the Load_Charges_IMPORT query, and this data is being imported from an excel document. Load_Charges_IMPORT查询中未设置任何键,并且此数据是从excel文档中导入的。

Can you tell me if there is something wrong with my code and why I am getting a response of 0 row(s) affected when I know there are 2600+ rows in Load_Charges_IMPORT . 您能告诉我我的代码是否有问题,为什么当我知道Load_Charges_IMPORT有2600多行时,为什么收到0 row(s) affected的响应0 row(s) affected

INSERT INTO Load_Charges
    SELECT *
    FROM Load_Charges_IMPORT
    WHERE 
        NOT EXISTS (SELECT [Load ID]
                    FROM Load_Charges
                    WHERE Load_Charges_IMPORT.[Load ID] = Load_Charges.[Load ID])
        AND NOT EXISTS (SELECT [Charge Description]
                        FROM Load_Charges
                        WHERE Load_Charges_IMPORT.[Charge Description] = Load_Charges.[Charge Description])
        AND NOT EXISTS (SELECT [Charged Amount] 
                        FROM Load_Charges
                        WHERE Load_Charges_IMPORT.[Charged Amount] = Load_Charges.[Charged Amount]);

Your EXISTS clause excludes all lines where any one of the conditions is TRUE, not only lines where all conditions are TRUE. 您的EXISTS子句排除其中任一条件为TRUE的所有行,不仅排除所有条件为TRUE的行。 Try this: 尝试这个:

INSERT INTO Load_Charges

SELECT *

FROM Load_Charges_IMPORT

WHERE NOT EXISTS (
    SELECT * 
    FROM Load_Charges 
    WHERE Load_Charges_IMPORT.[Load ID]=Load_Charges.[Load ID]
    AND Load_Charges_IMPORT.[Charge Description]=Load_Charges.[Charge Description]
    AND Load_Charges_IMPORT.[Charged Amount]=Load_Charges.[Charged Amount]);

Another way to address the issue is to use a series of LEFT JOIN s, with a WHERE clause that excludes any matching record. 解决该问题的另一种方法是使用一系列LEFT JOIN ,其WHERE子句排除任何匹配的记录。

This is a shorter syntax, and avoids using a subquery. 这是一种较短的语法,并且避免使用子查询。

INSERT INTO Load_Charges
SELECT imp.*
FROM 
    Load_Charges_IMPORT imp
    LEFT JOIN Load_Charges load1 ON load1.[Load ID] = imp.[Load ID]
    LEFT JOIN Load_Charges load2 ON load2.[Charge Description] = imp.[Charge Description]
    LEFT JOIN Load_Charges load3 ON load3.[Charged Amount]= imp.[Charged Amount]
WHERE load1.[Load ID] IS NULL AND load2.[Load ID] IS NULL AND load3.[Load ID] IS NULL
;

NB : this assumes that [Load ID] is a non-nullable field in the Load_Charges table. 注意:这假定[Load ID]是Load_Charges表中的不可为空的字段。 If not, any other non-nullable field can be used in the WHERE clause. 如果不是,则可以在WHERE子句中使用任何其他非空字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM