I have a a table called Process that has columns something like this
Hold, GId, Source, Type, Operation
and Temp_Table that looks something like this
Hold, GId, Source, Type
Hold_Table looks something like this
Hold, GId, Source, Type
I compare Temp_Table and Hold_Table and insert the record into Process table with "Add" Operation if the Source is not found in Hold_Inv table but I'm just wondering how can I prevent it from inserting duplicated value into Process table.
INSERT INTO Process (Hold, GId, Source, Type, Operation)
SELECT Hold, GID, Source, Type, 'ADD' Operation
FROM Temp_Table
WHERE Source IN (
SELECT Source
FROM (
SELECT Source
FROM Temp_Table Thi
WHERE NOT EXISTS (
SELECT 1
FROM Hold_Table Hi
WHERE Thi.Source = Hi.Source
)
AND Thi.Source <> 'NOT FOUND'
AND LEN(TRIM(ISNULL(Thi.Source, ''))) > 0
) T1
)
I run the query twice then the record from Temp_Table are inserted twice into the Process table.
It should be inserting twice if one column value is different but if they all the same then it shouldn't be inserting twice anymore.
Any help or suggestion would be really appreciated.