删除表b中Where =的表a

本文关键字:的表 Where 删除 | 更新日期: 2023-09-27 18:04:53

我有一个staging表,我想通过它删除我的Customers表中所有匹配的记录。在"语言术语"中:

delete 
   tableA.* 
from 
   table A,table B 
where 
    TableA.col1=TableB.col1 
    && TableA.colb=TableB.col2 /// and so forth

关于表的一些信息:

  • 表之间没有关系。匹配记录的唯一正确方法是匹配所有列(我想清除任何重复)
  • 两个表之间没有外键。Staging table是从CSV导入的,数据将被转换以在我们的系统中使用。
  • 大多数导入将是相同的(大约80%的staging行将从大约60k条记录中删除)

我在Linq2SQL中工作,但由于所有查询都需要更长的时间,并且每个查询都有大约80%的匹配记录,我觉得单个查询应该足够了。

这在SQL中是可能的吗?

删除表b中Where =的表a

可以将JOIN和DELETE同时使用

DELETE a
FROM tableA a
INNER JOIN tableB b
  ON  a.Col1 = b.Col1
  AND a.ColB = b.ColB 
  ... and so on

或使用EXISTS:

DELETE a
FROM tableA a
WHERE EXISTS
(
   SELECT 1 FROM tableB b
   WHERE a.Col1 = b.Col1
     AND a.ColB = b.ColB
     ....
)
merge table1 t1
    using (
        select t2.ID
            from table2 t2
    ) as d
    on t1.ID = d.ID
    when matched then delete;