2010-01-19 198 views

回答

1

最簡單的方法是EXISTS。我假設你想檢查訂單和OrderItems。這也意味着你只能過濾一次CustID。

UPDATE 
    C 
SET 
    IsDeleted = 1 
FROM 
    Customers C 
WHERE 
    C.CustID = 2 
    AND 
    EXISTS (SELECT * 
     FROM 
      Orders O 
     WHERE 
      O.CustID = C.CustID AND O.IsDeleted = 1) 
    AND 
    EXISTS (SELECT * 
     FROM 
      Orders O 
      JOIN 
      OrderItems OI ON O.OrderID = OI.OrderID 
     WHERE 
      O.CustID = C.CustID AND OI.IsDeleted = 1) 
+0

@gbn工作如果orderitems表中的某些行的Is_Deleted = 0 C.CustID = 2 – 2010-01-19 12:53:36

+0

什麼都沒有。 EXISTS是錯誤的 – gbn 2010-01-19 12:55:49

1

好吧聽起來不錯...... 難道我們只設置刪除標誌,當所有的訂單和所有與客戶相關的訂單項目被刪除,或者只在至少有1項被刪除。

+0

@Anthonyv我會設置刪除標誌,當所有的訂單和與該客戶相關的所有訂單項都被刪除 – 2010-01-19 12:43:57