first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

MS-SQL and T-SQL: Deleting Records Not in Another Table

Suppose Table1 has records that are not in Table2. Also suppose that Table1 and Table2 have a column named "IndexColumn" which can be used for an outer join.

We want an outer join because we want to delete the records in Table1 that are not in Table2. In MS-SQL we have:

DELETE Table1.*
FROM Table1 LEFT JOIN Table2
ON Table1.IndexColumn = Table2.IndexColumn
WHERE ((Table2.IndexColumn Is Null));

Do not do this in t-SQL. Use the following instead:

DELETE Table1
WHERE IndexColumn
NOT IN (SELECT IndexColumn FROM Table2)
mod date: 1999-02-09T19:36:41.000Z