title: Postgres Create Trigger Pass Parameter from Current Insert
body: <p>I'm very new to Postgres and right now I am tasked to create a trigger that would fire after an insert. Basically, I would let a script do an insert, fire the trigger that would grab a field from another table based on a parameter from the Insert Statement then update the newly inserted record with the value from the other table.</p>
  
  <ol>
  <li>INSERT record to Table 1.</li>
  </ol>
  
  <pre class="lang-sql prettyprint-override"><code>INSERT INTO "TableA" VALUES ('ABC-101', 'John Doe', '')
  
  PersonID  |  Name  |  Address
  ------------------------------
  ABC-101    John Doe 
  </code></pre>
  
  <ol start="2">
  <li>After this insert, I would then fire the trigger that would grab the value from a field in TableB.</li>
  </ol>
  
  <pre class="lang-sql prettyprint-override"><code>CREATE OR REPLACE FUNCTION fn_getaddress()
    RETURNS trigger AS
  $BODY$
  DECLARE
    PersonID     TEXT;
  BEGIN
    PersonID  := TG_ARGV[0];
  
      UPDATE  "TableA" 
         SET  "Address"  = (SELECT  "Address" 
                              FROM  "TableB" 
                             WHERE  "PersonID" = PersonID);
      RETURN null;
  END;
  $BODY$
    LANGUAGE plpgsql VOLATILE
    COST 100;
  </code></pre>
  
  <p>Here's my trigger:</p>
  
  <pre><code>CREATE TRIGGER tr_getaddress
  AFTER INSERT
  ON TableA
  FOR EACH ROW
  EXECUTE PROCEDURE fn_getaddress(personid);
  </code></pre>
  
  <p>So basically, I have a field in TableB (say Address) that I need to grab in order to complete the record in TableA. I am using an ID to reference which Address to take from TableB.</p>