0

I have try to make a trigger on a table call product. After inserting a product i get an error, that "NEW" variable is not affected yet.

Here is the trigger and user-defind function :

CREATE OR REPLACE FUNCTION updateProduitQteInitiale() RETURNS TRIGGER AS $example_table$
    BEGIN
       IF (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN      
       UPDATE produit set qtestock= NEW.qteinitial where produit.pid = NEW.pid ; 
       return NEW ;
      END IF;
      RETURN NULL;
    END;
$example_table$ LANGUAGE plpgsql;

And here is the trigger:

CREATE TRIGGER qteInitialTrigger AFTER INSERT OR UPDATE  ON produit
FOR EACH ROW EXECUTE PROCEDURE updateProduitQteInitiale();

1 Answer 1

1

Is pid your primary id? Not sure what you want to do but I assume you want to set to the column qtestock value from column qteinitial (on the same row!). If I guessed it right then you can do this:

CREATE OR REPLACE FUNCTION updateProduitQteInitiale() RETURNS TRIGGER AS $example_table$
BEGIN
  NEW.qtestock = NEW.qteinitial;     
  return NEW;
END;
$example_table$ LANGUAGE plpgsql;

CREATE TRIGGER qteInitialTrigger BEFORE INSERT OR UPDATE  ON produit
FOR EACH ROW EXECUTE PROCEDURE updateProduitQteInitiale();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.