SQL Help: Deleting orphan records

Liviathan

Platinum Member
Feb 21, 2001
2,286
0
0
Got 2 tables... Content_grid and testresults.

They share 2 fields. TPID and TrackingNUmber

I want to delete from contentgrid the records that do not appear in testresult, using both fields. So if contentgrid has a record tpid=2 trackingnumber=44 and that record is not in testresult I want to deleted.

Any help would be great....thanks.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
delete from CONTENT_GRID where not exists (select 1 from TESTRESULTS where TESTRESULTS.TPID = CONTENT_GRID.TPID and TESTRESULTS.TrackingNumber = CONTENT_GRID.TrackingNumber);

EDIT: Change the "delete" to "select *" before running the query to ensure that it's deleting the records you want.