user53885
user53885

Reputation: 3829

Delete from table the records that are in #temp table

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

Upvotes: 1

Views: 23185

Answers (2)

Tobiasopdenbrouw
Tobiasopdenbrouw

Reputation: 14039

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)

Upvotes: 2

Vidar Nordnes
Vidar Nordnes

Reputation: 1364

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)

Upvotes: 14

Related Questions