在一个表中删除记录,这些记录与另一个表中的记录相同

时间:2014-04-17 19:00:01

标签: sql sql-server ssis

我有一个与某些列匹配的SSIS包,并将其传输到“匹配”表中,因为报告来自供应商。

报告进来,数据放入“传入”表中。在这之后。我加入某些列和某个列不是NULL(匹配的列)的记录,并将它们放入“匹配”表中。

我的问题是:

如何删除与我加入的表格匹配的记录?

以下是我要从“Incoming”表中删除的记录的连接声明:

SELECT * FROM Incoming I
LEFT OUTER JOIN Mathced M
ON I.Name = M.Name AND I.Type = M.Type AND I.Op = M.Op AND I.DSource = M.DSource AND IDate = M.Date
WHERE M.MatchedID IS NOT NULL

3 个答案:

答案 0 :(得分:2)

试试这个:

DELETE FROM Incoming I
LEFT OUTER JOIN Mathced M
  ON I.Name = M.Name
    AND I.Type = M.Type
    AND I.Op = M.Op
    AND I.DSource = M.DSource
    AND I.Date = M.Date
WHERE M.MatchedID IS NOT NULL

答案 1 :(得分:1)

DELETE FROM Incoming I
LEFT OUTER JOIN Mathced M
ON I.Name = M.Name AND I.Type = M.Type AND I.Op = M.Op 
                   AND I.DSource = M.Source AND IDate = M.Date
WHERE M.MatchedID IS NOT NULL

答案 2 :(得分:0)

这是正确答案:

DELETE I FROM Incoming I LEFT OUTER JOIN Matched M ON I.Name = M.Name AND I.Type = M.Type AND I.Op = M.Op AND I.DSource = M.DSource AND I.Date = M.Date WHERE M.MatchedID IS NOT NULL

......因为你首先必须告诉DBMS你要从哪个表中删除,然后使用别名来说明JOIN