从 SQL Server 2008 R2 中的实际数据库 Table 中删除非重复行

Delete the non-duplicate row from actual Database Table in SQL Server 2008 R2

我有一个 table 变量 @Temp 和一个数据库 table Master

这是 table 中的示例数据:

@Temp:

EMPID      ID
1           1
1           3
2           2
2           3

Master:

EMPID     ID
1          1
1          2
1          3
2          1
2          2
2          3
3          1
3          2

现在我必须从 Master table 中删除 EMPID 和 ID 对与 @temp table 不匹配的行。它应该删除 2 行(@Temp table 中不存在这对行)。我还想保留@Temp table 中不存在EMPID 的此类记录。删除记录后我的主人 table 应该有以下记录。

输出

Master

EMPID      ID

1          1
1          3
2          2
2          3
3          1
3          2

你可以试试这个

DELETE FROM Master WHERE EMPID IN (SELECT EMPID FROM @Temp) AND NOT EXISTS 
(SELECT 1 FROM @Temp WHERE EMPID=Master.EMPID AND ID = Master.ID) 

如有帮助请标记为答案