Hi everyone,
I have a SQL statement I'd like to amend. As it stands now I'm running a simple SELECT INTO but I'd like to modify it so only records that don't exist in the destination table (as determined by my criteria) are appended.
Here's my initial statement:
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())
I'd like to change things so that the StagingTable is populated only when the ballot doesn't already exist. Is this an acceptable way of going about it, or are there better alternatives?
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)) )