tags:

views:

34

answers:

2

I have a bunch of data in a table that serves as the parent (primary key in foreign key relationship) to several other tables. I basically want to go through this table and try and delete records that do not have children. I have already deleted the children, so there are parents that are childless.

If a record can be deleted, delete it, otherwise move onto the next record and don't show me that cannot delete error.

This isn't any kind of production need, I was just messing around with some test code and deleted some children, now I want to delete their sad parents.

This can either be done in sql or C# linq stuff. I basically just want to clear out my irrelevant data.

+3  A: 

You could always try a query like

DELETE FROM ... WHERE NOT EXISTS (...
Phil Brown
+1  A: 

You can user an outer join - lets say you have master and child joining on id

delete m
from master m
left outer join child c 
on m.id = c.id
where c.id is null

What this does is join up all the records of master with the child and where the child doesn't exist it puts nulls in. You just need to select the ones where the child is null

Preet Sangha