tags:

views:

46

answers:

2

Hello,

I've created a #temp table in SQL containing duplicate records.

I would like to remove from my primary table all of the records contained in this temp table. I see samples to do this but they seem to all invovle selects, and I already have my select in the temp table.

Here's what it would look like in pseudocode:

DELETE FROM MyMainTable(unique and duplicate records) WHERE the record exists in #temp
+4  A: 
DELETE T
FROM MyMainTable T
INNER JOIN #temp ON T.id = #temp.id

You can also do:

DELETE T
FROM MyMainTable T
WHERE EXISTS (SELECT *
                FROM #temp 
                WHERE T.id = #temp.id)
Vidar Nordnes
Thank you for the help, this works wonderfully.
+2  A: 

As global a level as your question:

If your temptable has the primary id's that the original had

Delete From Originaltable where primarykeyid in (select primarykeyid from temptable)
Tobiasopdenbrouw
IMO you generally get better performance using an `EXISTS` with a `JOIN` than with an `IN`
Raj More
Thank you appreciate the help.