更新POSTGRESQL后触发

时间:2014-06-20 19:51:49

标签: postgresql set sql-update

我有下一个问题,当我将estado = false更新为estado = true时,我尝试使用触发器函数设置列n_colaboradores,但是当我这样做时,n_colaboradores保持为零并且不会增加其值。触发器sintax或函数可能有问题吗?救命 ! :d

CREATE TABLE Postula (
id_postulante   int references Postulante(id_postulante) not null,
id_area         int references Area(id_area) not null,
estado          boolean not null,
prioridad       int not null,
primary key(id_postulante, id_area)
);

 CREATE TABLE Area (
id_area         SERIAL primary key not null,
nombreA         varchar(100) not null,
n_colaboradores int not null,
horario         varchar(100) not null,
campusA         varchar(100) not null
 );

 CREATE OR REPLACE FUNCTION AumentarColaboradores()
 RETURNS trigger AS '

 BEGIN

SELECT *FROM AREA a,POSTULA p WHERE a.id_area = p.id_area;

IF (NEW.p.estado IS TRUE)
THEN
    UPDATE AREA
    SET N_COLABORADORES = N_COLABORADORES + 1;
END IF;

END;' LANGUAGE 'plpgsql'

CREATE TRIGGER Trigger_num_seleccionados
        AFTER UPDATE ON POSTULA
        FOR EACH ROW
        EXECUTE PROCEDURE AumentarColaboradores();

1 个答案:

答案 0 :(得分:0)

我不确定触发器功能中的SELECT *FROM AREA a,POSTULA p WHERE a.id_area = p.id_area;是什么,但是如果您只想更新正在更新的表上的另一个表,那么您的列" estadio"是的,你可能只需要这样简单的东西:

create or replace function AumentarColaboradores() returns trigger AS 
$$
BEGIN
    IF (NEW.estado = TRUE) THEN
        UPDATE area
        SET N_COLABORADORES = N_COLABORADORES + 1;
        --no WHERE in this update? it will update the whole table

        -- also, (uncomment below) you may want to take 1 away if estadio is false

--  ELSE
--      UPDATE area
--      SET N_COLABORADORES = N_COLABORADORES - 1;
    END IF;

    RETURN NEW;
END
$$
language plpgsql