title: SQL trigger to delete from another table
body: <p>I have two tables:</p>
  
  <p><strong>Products</strong>:</p>
  
  <pre><code>Product ID | Quantity
  </code></pre>
  
  <p><strong>OrdersLines</strong>:</p>
  
  <pre><code>Product ID | Amount --(multiple lines with the same ID)
  </code></pre>
  
  <p>Naturally, before I delete the product from the <code>Products</code> table, I need to delete all order lines with that product id first from the <code>OrdersLines</code> table.</p>
  
  <p>So I wrote a trigger to do it:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>However, when I try do delete in the form from Products table, it still says: </p>
  
  <blockquote>
    <p>There are rows in a related table (Sales OrdersLines) that must be removed before this row can be deleted</p>
  </blockquote>
  
  <p>What am I doing wrong here?</p>
  
  <p>EDIT as per request: Main constraint: </p>
  
  <pre><code>[dbo].[atbl_Sales_OrdersLines]  WITH CHECK ADD  CONSTRAINT 
  [FK_atbl_Sales_OrdersLines_atbl_Sales_Products] FOREIGN KEY([ProductID])
  REFERENCES [dbo].[atbl_Sales_Products] ([ProductID])
  </code></pre>