使用子查询删除并相交?

Delete using subquery and intersect?

我正在尝试使用以下查询删除,但不幸的是,所有记录都被删除了

DELETE [dbo].[devicelinks]
FROM   
    (SELECT *
     FROM [dbo].[devicelinks]
     WHERE PID = 7

     INTERSECT 

     SELECT * 
     FROM ASC.dbo.DEVICE_LINK
     WHERE PID = 7)

如何正确使用子查询从源中删除对应的记录table?

两个 table 中的任何一个都没有键或约束。

您可以使用EXISTS检测要删除的记录:

DELETE d1
FROM [dbo].[devicelinks] d1
WHERE PID = 7 AND EXISTS (SELECT *
                          FROM ASC.dbo.DEVICE_LINK AS d2
                          WHERE d1.PID = d2.PID AND
                                d1.[DEVICEID] = d2.[DEVICEID] AND
                                ... rest of the fields here )

试试这个

delete [dbo].[devicelinks]
from [dbo].[devicelinks]
    inner join ASC.dbo.DEVICE_LINK
     on [dbo].[devicelinks].PID = ASC.dbo.DEVICE_LINK.PID
where [dbo].[devicelinks].PID = 7