SQL删除没有子记录的父记录

时间:2014-11-27 20:56:55

标签: sql-server

我正在尝试删除与CombinableOrders无关联的所有Orders

我的尝试

DELETE 
 FROM CombinableOrders pr    
 WHERE NOT EXISTS(SELECT * FROM Orders cr WHERE pr.Id = cr.CombinableOrder_Id)

Sql server错误

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

我知道这一定很简单

更新

我认为pr和cr导致了这个问题,并且sql在前一个语句中给了我一个错误

2 个答案:

答案 0 :(得分:0)

试试这个:

DELETE
    CombinableOrders 
FROM 
    CombinableOrders
WHERE 
    NOT EXISTS(SELECT 1 FROM Orders WHERE CombinableOrders.Id = Orders.CombinableOrder_Id)

答案 1 :(得分:0)

试试这个: 删除  来自CombinableOrders pr
 WHERE((SELECT COUNT(*)FROM Orders cr WHERE pr.Id = cr.CombinableOrder_Id)= 0)