Access 2010查询删除存在不起作用的地方

时间:2013-07-25 15:54:50

标签: sql ms-access sql-delete

我必须连接两个表并从第二个表中不存在的一个表行中删除。我正在尝试下面的查询。我应该只删除7条记录但删除整个PERSONS表。

DELETE PERSONS.*
FROM PERSONS
WHERE EXISTS (
    SELECT PERSONS.[IR NUMBER],
      PERSONS.[Last Name],
      PERSONS.[First Name],
      PERSONS.DOB,
      PERSONS.[Name Type]
    FROM 1 tblPERSON
    RIGHT JOIN PERSONS
      ON (
             iif(isnull([1tblPERSON].DOB) = true, "01/01/9999", [1tblPERSON].DOB) = 
             iif(isnull(PERSONS.DOB) = true, "01/01/9999", PERSONS.DOB)
         )
        AND ([1tblPERSON].FIRSTNAME = PERSONS.[First Name])
        AND ([1tblPERSON].LASTNAME = PERSONS.[Last Name])
        AND ([1tblPERSON].NAMETYPELIT = PERSONS.[Name Type])
        AND ([1tblPERSON].INCINMBR = PERSONS.[IR Number])
    WHERE (
        (
          (PERSONS.[IR Number]) IN (
            SELECT [QryIncinmbrsForDelete] ! [INCINMBR]
            FROM [QryIncinmbrsForDelete]
            )
          )
        AND (
          (PERSONS.[Last Name]) NOT IN (
            "UNKNOWN",
            "UNK",
            "UKNOWN"
            )
          )
        AND ((IsNull([1tblPERSON].[INCINMBR])) = True)
        )
    ) = true;

编辑:

在这个例子中... 1,DOE,JOHN,1/1/1960,Cust将被删除

1tblPERSON:

INCINMBR Last Name First Name DOB      NameType 
1        DOE       JON        1/1/1960 Cust 
1        SMITH     JOHN       2/2/1965 Emp 
1        JONES     JAY        3/3/1965 Cust 

PERSONS:

INCINMBR Last Name First Name DOB      NameType 
1        DOE       JON        1/1/1960 Cust 
1        SMITH     JOHN       2/2/1965 Emp 
1        JONES     JAY        3/3/1965 Cust 
1        DOE       JOHN       1/1/1960 Cust 

1 个答案:

答案 0 :(得分:0)

问题是你没有告诉删除语句从PERSONS中删除什么,如果你的子查询返回任何结果,你告诉它删除所有内容(据我所知,你期望它返回7?)

如果您可以提供有关您的密钥的更多信息,那么我可以为您提供完整的解决方案。