我有一条 SQL 语句要修改。就目前而言,我正在运行一个简单的 SELECT INTO 但我想对其进行修改,以便仅附加目标表中不存在的记录(由我的标准确定)。
这是我最初的声明:
SELECT b.BallotID, m.MeetingDate
INTO StagingTable
FROM Ballot INNER JOIN Meeting on b.MeetingID = m.MeetingID
WHERE b.LastModifiedDate < '01-01-2010' AND ( b.BallotType = 'Agenda Vote' AND m.MeetingDate < GETDATE())
我想更改一些内容,以便仅在选票尚不存在时填充 StagingTable。这是一种可以接受的方式,还是有更好的选择?
SELECT b.BallotID, m.MeetingDate
INTO StagingTable
FROM Ballot INNER JOIN Meeting on b.MeetingID = m.MeetingID
WHERE b.LastModifiedDate < '01-01-2010' AND ( b.BallotType = 'Agenda Vote' AND m.MeetingDate < GETDATE())
AND NOT EXISTS(SELECT 1 from StagingTable where BallotID = b.BallotID)) )