title: PostgreSQL: Trigger Procedures
body: <p>I have these two tables in my postgresql database:<br></p>
  
  <pre><code>CREATE TABLE credit
  (
    id serial NOT NULL,
    id_emp integer,
    amount double precision,
    refund double precision,
    rest double precision,
    stat integer,
    CONSTRAINT credit_pkey PRIMARY KEY (id)
  )
  </code></pre>
  
  <p>and this:<br></p>
  
  <pre><code>CREATE TABLE emp_with_credit
  (
    id serial NOT NULL,
    id_emp integer,
    stat integer,
    CONSTRAINT emp_with_credit_pkey PRIMARY KEY (id)
  )
  </code></pre>
  
  <p>I want to create a procedure trigger <code>ON UPDATE</code> that does this work:<br>
  When I update a row the <code>credit</code> table I have to check if the rest = 0 then : set the <code>stat</code> column of the credit table = 0 and get the id_emp of the updated row and set the stat column of emp_with_credit table = 0 when credit.id_emp = emp_with_credit.id_emp<br><br>
  <strong>EDIT:</strong><br>
  What I did so far:<br></p>
  
  <pre><code>CREATE FUNCTION credit_stamp() RETURNS trigger AS $credit_stamp$
      BEGIN
          IF NEW.rest = 0 THEN
          NEW.stat = 0;
      END IF;
          RETURN NEW;
      END;
  $emp_stamp$ LANGUAGE plpgsql;
  
  CREATE TRIGGER credit_stamp BEFORE UPDATE ON credit
      FOR EACH ROW EXECUTE PROCEDURE credit_stamp();
  </code></pre>
  
  <p>This works so good but still the second part where I have to set the <code>stat</code> column of <code>emp_with_credit</code> table = 0 when <code>credit.id_emp = emp_with_credit.id_emp</code>.</p>