In this example, we want to delete from CustomerSales where CustomerID no longer exists in the Customers table:
DELETE dbo.CustomerSales a
WHERE NOT EXISTS(
SELECT 1 FROM dbo.Customers b
WHERE a.CustomerID = b.CustomerID)
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'a'.
This is the correct syntax:
DELETE a
FROM dbo.CustomerSales
a
WHERE NOT EXISTS(
SELECT * FROM dbo.Customers b
WHERE a.CustomerID = b.CustomerID)
Hopefully useful for somebody!
No comments:
Post a Comment