I currently have a SQL insert into statement that copies items from one identical table to another.
The source table has many identical records or records that may have small variations.
Example…
The source and destination table are in the format:
ItemDATE // ItemTIME // SITENAME // SERIAL_NO // RPM // Power // Voltage1 // Voltage2 // etc
The source may contain:
ItemDATE // ItemTIME // SITENAME // SERIAL_NO // RPM // Power // Voltage1 // Voltage2 // etc
2013-01-01 00:00:00.000 // 17:00:00.1 // Oxford College // 0014617 // 1500 // 250 // 250.8 // 249.2
2013-01-01 00:00:00.000 // 17:00:00.1 // Oxford College // 0014617 // 1499// 249 // 253.5 // 240.6
2013-01-01 00:00:00.000 // 17:00:00.1 // Oxford College // 0014617 // 1502// 251 // 239.8 // 269.8
My insert statement is:
INSERT INTO ReportingSystem.dbo.HistoryLog(ItemDATE, ItemTIME, SITENAME, SERIAL_NO, RPM, Power, Voltage1, Voltage2)
SELECT ItemDATE, ItemTIME, SITENAME, SERIAL_SN, RPM, Power, Voltage1, Voltage2
FROM ReportingSystem.dbo.RTCU
EXCEPT
SELECT ItemDATE, ItemTIME, SITENAME, SERIAL_SN, RPM, Power, Voltage1, Voltage2
FROM ReportingSystem.dbo.HistoryLog
Where HistoryLog is the destination and RTCU is the source.
I am not concerned about the different values in the RPM, Power, Voltage1 and Voltage2 fields but there should be no records where the itemDATE, ItemTIME, SITENAME and SERIAL_NO are the same.
But with the Except statement I am still getting duplicated as it considers the entire record.
I have tried the following with no success:
INSERT INTO HistoryLog
(ItemDATE, ItemTIME, SITENAME, SERIAL_NO, RPM, Power, Voltage1, Voltage2)
SELECT ItemDATE, ItemTIME, SITENAME, SERIAL_NO, RPM, Power, Voltage1, Voltage2
FROM RTCU
WHERE not exists (select * from HistoryLog
WHERE HistoryLog.ItemDATE = rtcu.ItemDATE
and HistoryLog.ItemTIME = rtcu.ItemTIME
and HistoryLog.SITENAME = rtcu.SITENAME
and HistoryLog.SERIAL_NO= rtcu.SERIAL_NO
);
Please help….
1 Answer