触发产品库存

时间:2015-03-06 11:00:11

标签: postgresql triggers plpgsql

我创建了一个触发器,每当我将lot添加到表格中时,我会在article表中添加此金额,但我的错误表明注册(新)项目

CREATE TABLE article (
  "idArticle" integer NOT NULL,
  "desArticle" character varying,
  "famiArticle" integer,
  "photoArticle" character varying,
  "stkPhArticle" integer,
  "stkThArticle" integer,
  "seuSurArticle" integer,
  "seuAlrArticle" integer,
  "seuReapArticle" integer,
  "phtAchaArticle" double precision,
  "phtVentArticle" double precision,
  "pttcArticle" double precision,
  "tvaArticle" integer,
  CONSTRAINT pk_idarticle PRIMARY KEY ("idArticle"),
  CONSTRAINT fk_famiarticle FOREIGN KEY ("famiArticle")
      REFERENCES famillearticle (idfa) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_tvaarticle FOREIGN KEY ("tvaArticle")
      REFERENCES tvaarticle (idtva) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
);

CREATE TABLE lot (
  "idLot" integer NOT NULL,
  "idArticle" integer NOT NULL,
  "emplacementLot" integer NOT NULL,
  "qteLot" integer NOT NULL,
  "dlcLot" date NOT NULL,
  CONSTRAINT pk_idlot PRIMARY KEY ("idLot"),
  CONSTRAINT fk_emplacementlot FOREIGN KEY ("emplacementLot")
      REFERENCES etagere ("idEtag") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_idarticle FOREIGN KEY ("idArticle")
      REFERENCES article ("idArticle") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
);

触发和功能:

CREATE OR REPLACE FUNCTION ajouter_lot_article()
      RETURNS trigger AS
    $BODY$
        BEGIN

        UPDATE article SET stkThArticle = stkThArticle + NEW.qteLot
        WHERE article.idArticle = New.idArticle;

        END;
    $BODY$
      LANGUAGE plpgsql;

CREATE TRIGGER apres_ajout_lot
  AFTER INSERT
  ON lot
  FOR EACH ROW
  EXECUTE PROCEDURE ajouter_lot_article();
  

erreur:“NEW”n'a pas de champs“idarticle”

1 个答案:

答案 0 :(得分:1)

列名称区分大小写,因此您需要将触发器中的列放在双引号内。

 CREATE OR REPLACE FUNCTION ajouter_lot_article()
      RETURNS trigger AS
    $BODY$
        BEGIN

        UPDATE article SET "stkThArticle" = "stkThArticle" + NEW."qteLot"
        WHERE article."idArticle" = New."idArticle";

        END;
    $BODY$
      LANGUAGE plpgsql VOLATILE
      COST 100;
    ALTER FUNCTION ajouter_lot_article()
      OWNER TO postgres;