I have two tables:
Products:
Product ID | Quantity
OrdersLines:
Product ID | Amount --(multiple lines with the same ID)
Naturally, before I delete the product from the Products
table, I need to delete all order lines with that product id first from the OrdersLines
table.
So I wrote a trigger to do it:
CREATE TRIGGER [dbo].[atbl_Sales_Products_DTrig]
ON [dbo].[atbl_Sales_Products]
FOR DELETE
AS
BEGIN
DELETE FROM atbl_sales_OrdersLines
WHERE ProductID = (SELECT ProductID FROM deleted)
END
However, when I try do delete in the form from Products table, it still says:
There are rows in a related table (Sales OrdersLines) that must be removed before this row can be deleted
What am I doing wrong here?
EDIT as per request: Main constraint:
[dbo].[atbl_Sales_OrdersLines] WITH CHECK ADD CONSTRAINT
[FK_atbl_Sales_OrdersLines_atbl_Sales_Products] FOREIGN KEY([ProductID])
REFERENCES [dbo].[atbl_Sales_Products] ([ProductID])