How do I delete all the parents that do not have a children

Posts   
 
    
Posts: 20
Joined: 23-Feb-2005
# Posted on: 29-Jun-2005 21:52:40   

Table Child ID PARENT_FK

Table Parent ID

I want to delete all of the parent rows who do NOT have children. How would I do that?

Richard Cherry

jtgooding
User
Posts: 126
Joined: 26-Apr-2004
# Posted on: 29-Jun-2005 22:22:37   

Its been a long day so test the sql in your favorite query tool to verify it =P

But your SQL is something like this, you should be able to use this to generate a predicate..

SELECT * FROM dbo.Parent P (nolock) LEFT JOIN dbo.Child C (nolock) on C.ParentKey = P.ParentKey WHERE C.ParentKey is null -- can actually be any field in the child since all fields return null

Just add the relation with a left hint and the compare predicate.

John

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 29-Jun-2005 22:27:49   

Execute a ParentCollection.DeleteMulti(filter, relation) call, where filter is a filter on Parent.ID is NULL and the relation collection contains the relation Parent-Child with a joinhint LEFT.

(edit), heh, John was slightly faster simple_smile

Frans Bouma | Lead developer LLBLGen Pro