Delete Parent record if child is not present

后端 未结 3 1310
猫巷女王i
猫巷女王i 2021-01-18 15:54

I am creating menu and submenus in a table variable. ( Typical parent child records in the same table) ParentID coll is null for all Top menus. And for their child ParentID

3条回答
  •  天命终不由人
    2021-01-18 16:40

    A single statement like this should do it (as an alternative to using exists):

    delete p
    from
        tblMenu p
        left join tblMenu c on p.ID = c.ParentID
    where 
        p.ParentID is null --Ensures that the item to delete is at the top
        and c.ParentID is null --Finds top level items with no children
    

提交回复
热议问题