触发增加值

时间:2014-07-30 11:56:03

标签: sql triggers

触发器有问题。

他不行。我有这张桌子:

create table Establishment (
    idEstablishment serial primary key,
    idBeacon int,
    "name" varchar(45) not null,
    address varchar(200) not null,
    urlImage varchar(200),
    description varchar(200),
    phoneNumber varchar(10) not null,
    "type" int not null,
    latitude float,
    longitude float, 
    indexVersion int not null,
    constraint fk_establishment_beacon foreign key (idBeacon) references Beacon (idBeacon));

我想增加indexVersion,其中信息被更新。 所以我使用触发器。

CREATE OR REPLACE FUNCTION inc_indexVersion()

RETURNS trigger AS

$BODY$
BEGIN
    UPDATE establishment SET NEW.indexversion = OLD.indexversion +1;
    RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql;

CREATE TRIGGER inc_trigger
  AFTER UPDATE
  ON Establishment
  FOR EACH ROW
  EXECUTE PROCEDURE inc_indexVersion();

但问题出在NEW.indexversion上。

我该怎么办? THX

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

CREATE OR REPLACE FUNCTION inc_indexVersion()

RETURNS trigger AS

$BODY$
BEGIN
    NEW.indexversion = OLD.indexversion +1;
    RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql;

CREATE TRIGGER inc_trigger
  BEFORE UPDATE
  ON Establishment
  FOR EACH ROW
  EXECUTE PROCEDURE inc_indexVersion();

THX。