title: Trigger function updates all rows instead of one (PostgreSQL)
body: <p>Using PostgreSQL.
  What I have is:
  <strong>3 tables</strong></p>
  
  <p>1.<code>Customer2c</code> with columns: <strong><code>CustomerID</code></strong>,<code>PersonID,</code>Number_Of_Items`. </p>
  
  <p>2.<code>SalesOrderHeader2c</code> with columns: <strong><code>SalesOrderID</code></strong>,<code>CustomerID</code>. </p>
  
  <ol start="3">
  <li><code>SalesOrderDetail2c</code> with columns: <strong><code>SalesOrderDetailID</code></strong>,<code>SalesOrderID</code>,<code>OrderQty</code></li>
  </ol>
  
  <p>I want to create a trigger function, that will trigger whenever someone uses </p>
  
  <p><code>INSERT INTO 'SalesOrderDetail2c' table</code></p>
  
  <p>and that is going to get the <code>OrderQty</code> that was inserted and update the correspondent <code>Number_Of_Items</code> field with it.</p>
  
  <p>My trigger is working, but <strong>the problem is</strong> that whenever I insert a new value to the SalesOrderDetail2c, the function gets the <code>OrderQty</code> value and <strong>updates all the rows of <code>Number_Of_Items</code> with it, instead of updating just the correspondent one.</strong></p>
  
  <p>Any help appreciated. What I have so far is this (It may be copletely wrong, dont judge please!):</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION FunctionTrigger2c() RETURNS TRIGGER AS
  $BODY$
  BEGIN
      UPDATE Customer2c
      SET Number_Of_Items = 
      (SELECT OrderQty
      FROM SalesOrderDetail2c
      INNER JOIN SalesOrderHeader2c ON (SalesOrderDetail2c.SalesOrderID = SalesOrderHeader2c.SalesOrderID)
      INNER JOIN Customer2c ON (SalesOrderHeader2c.CustomerID = Customer2c.CustomerID)
      ORDER BY SalesOrderDetailID DESC LIMIT 1
      )
      FROM SalesOrderHeader2c
      WHERE SalesOrderHeader2c.CustomerID = Customer2c.CustomerID
      ;
      RETURN NEW;
      END;
  $BODY$ 
  language plpgsql;
  
  CREATE TRIGGER Trigger2c
       AFTER INSERT ON SalesOrderDetail2c
       FOR EACH ROW
       EXECUTE PROCEDURE FunctionTrigger2c();
  </code></pre>